What is the structure of PHP code?

PHP is an embedded scripting language; this means that it is possible to write PHP code into an HTML file. Since web browsers can only process HTML files, the web-server converts and embeds the PHP code into one HTML file before sending it to the browser.

svg viewer

HTML tags

There are HTML tags for PHP code to indicate the start and end of PHP code in an HTML file. Any one of the following 4 tags can be used:

  1. <?php php-code-here ?>
  2. <SCRIPT LANGUAGE="php"> php-code-here </SCRIPT>
  3. <? php-code-here ?>
  4. <% php-code-here %>

The first and second tags are the ones most recommended and most widely used. Using a tag which is rarely used may result in a web-server being unable to detect the start and end of the PHP code.


Commenting

# and // are used to comment out a single line of code, while /* and */ indicate the start and end of a commented block of code.


Code

<?php
print "Hello";
echo " World!\n";
/* Commenting out a block of code
echo 'This line won't execute.\n';
*/
# The last line does not require a semicolon
print "The last line."
?>
Copyright ©2024 Educative, Inc. All rights reserved