PHP Syntax

PHP script can be placed anywhere in a page.

PHP script always starts with “<?php” and always ends with “?>“.

NOTE: Sometimes it is used in short form as opening with “<?” and closing with “?>”, but writing it in its explicit full form is recommended.

Example of a PHP script in a HTML page:

<html>
<head>
</head>
<body>
<?php echo 'Hello'; ?>
</body>

</html>

Other important syntax in PHP:

  • Every sentence ends with a semi-colon “;” 
  • Comments can be written in two ways:
    • For single line comment : “//” is used

      Example : //Function for addition

    • For multiple line comment : “/* ……… */” is used.

      Example : /*Function for addition
      Parameters taken by the function : a, b */

      Example in a HTML/PHP page

      <html>
      <body><?php
      //This is a single line comment

      /*
      This is a
      multiple-line
      coment
      */
      ?>

      </body>
      </html>

  • For printing anything, or for printing the output “echo” is used.Example: echo ‘Hello World!’;

    Example: echo $y;

  • For writing variables, we use “$” signExample: $y, $string1, $first_name etc.

Leave a Reply

Your email address will not be published. Required fields are marked *