
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP Escaping From HTML
Definition and Usage
PHP file can have mixed content with code within tags embedded in a HTML document. Code outside tags is ignored by the parser, leaving it to be interpreted by client browser. A HTML document can have multiple blocks of PHP, each inside <?php and ?> tags.
Syntax
<p> HTML block </p> <?php //php block .. .. ?> <p> HTML block </p> <?php //php block .. .. ?> <p> HTML block </p>
Every time opening PHP tag is encountered, parser starts rendering the output to the client until closing tag is reached. If code consusts of conditional statement, th parser determines which block to be skipped.
Again till another opening tag comes, everything is treated as HTML leaving the browser to process the same.
PHP Version
This description is applicable to all versions of PHP.
Following example shows PHP code embedded in HTML
Example
<html> <body> <!..HTML code--!> <h3>Hello World</h3> <!-- PHP code --!> <?php echo "Hello World in PHP"; ?> <!-- This is HTML code --!> <p>Hello world again</p> <?php echo "Hello World again in PHP"; ?> </body> </html>
Output
This will produce following result −
Hello World Hello World in PHP Hello world again Hello World again in PHP
Example using mixed HTML and PHP code
Example
<?php $marks=10; ?> <h1>Using conditional statement</h1> <?php if ($marks >=50): ?> <h2 style="color:blue;">Result:pass</p> <?php else: ?> <h2 style="color:red;"> Result:Fail</p> <?php endif; ?>
Output
This will produce following result −
Using conditional statement Result:Failchange marks to 60 and run again Using conditional statement Result:pass
- Related Articles
- Escaping Characters in Perl
- Swift optional escaping closure parameter
- Escaping Characters in Bash on Linux
- Parse HTML with PHP's HTML DOMDocument
- Escaping quotes while inserting records in MongoDB?
- What is the use of Escaping in LESS?
- Can HTML be embedded inside PHP “if” statement?
- How to display XML in HTML in PHP?
- Simple Contact Form using HTML CSS and PHP
- Escaping/encoding single quotes in JSON encoded HTML5 data attributes
- PHP Variables from External Sources
- PHP: Remove object from array
- Run a Python program from PHP
- Detect language from string in PHP
- PHP print keys from an object?
