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:Fail

change marks to 60 and run again Using conditional statement Result:pass

Updated on: 19-Sep-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements