Thursday, May 7, 2020

Embedding PHP in HTML

PHP is an HTML-embedded server-side scripting language. When building a complex page, at some point you will be faced with the need to combine PHP and HTML to achieve your needed results. At the first point, this can seem complicated, since PHP and HTML are two separate languages, but this is not the case. PHP is designed to interact with HTML and PHP scripts can be included in an HTML page without a problem.

In an HTML page, PHP code is enclosed within special PHP tags. When a visitor opens the page, the server processes the PHP code and then sends the output (not the PHP code itself) to the visitor's browser. Actually, it is quite simple to integrate HTML and PHP. A PHP script can be treated as an HTML page, with bits of PHP inserted here and there. Anything in a PHP script that is not contained within <?php ?> tags is ignored by the PHP compiler and passed directly to the web browser. If you look at the example below you can see what a full PHP script might look like:

Recommended usage:

<html>
<head>
</head>
<body class="page_bg">
Hello, today is <?php echo date('l, F jS, Y'); ?>.
</body>
</html>


The code above is simply HTML, with just a bit of PHP that prints out today's date using the built-in date function. As mentioned above, all of the plain HTML in the code above will be ignored by the PHP compiler and passed through to the web browser untouched.

 More advanced techniques:

<html>
<head></head>
<body>
<ul>
<?php for($i=1;$i<=5;$i++){ ?>
<li>Menu Item <?php echo $i?></li>
<?php } ?>
</ul>
</body>
</html>

and the result is:

· Menu Item 1
· Menu Item 2
· Menu Item 3
· Menu Item 4
· Menu Item 5

 

PHP in HTML using short_open_tag

If you want to shorten your code as much as possible, you can go for the short_tags option. This will save you from typing <?php at the beginning of the code, shortening it to just <?.

PHP in HTML using short_tags:

<html>
<head></head>
<body class="page_bg">
Hello, today is <?=date('l, F jS, Y'); ?>.
</body>
</html>

Have in mind that if you want to build a website compatible with as many platforms as possible, you should not rely on short_tags.

HTML in PHP using echo

A possible way to integrate HTML tags in a PHP file is via the echo command:

Possible yet not recommended usage:

<?php
echo "<html>";
echo "<head></head>";
echo "<body class=\"page_bg\">";
echo "Hello, today is ";
echo date('l, F jS, Y'); //other php code here echo "</body>";
echo "</html>";
?>

This will, however, affect the HTML Code Coloring option in most HTML/PHP editors, which allows for an easy understanding of the role of HTML tags. You should escape each double quote within the HTML code with a backslash.


Comments in PHP

The word comment itself expresses its meaning as commenting out something. If we comment on anything in the PHP program file, it will not be compiled with the code. The compiler or the interpreter will simply ignore this.

There are two types of comments you can add:

1.     Single line comment used for quick notes about complex code or to temporarily disable a line of PHP code. You need to add // or # before the code.

Example-1 :

<?php
echo "This is my first PHP Program";
// this is the first program
?>

 

Example-2 :

<?php
# $i=10;
# $j=20;
# echo $i + $j;
echo "Hello World!";
# this is PHP comment
?>

 

2.     Multi-line comment used to comment out large blocks of code or writing multiple line comments. You need to add /* before and */ after the code.

Example:

<?php
    /* The following line of code
       will output the "Hello World!" message */
    echo "Hello World!";
?>