PHP Syntax

PHP

In this tutorial we will learn about PHP syntax.

The PHP script

A PHP script starts with a <?php and ends with a ?>. Anything written written within those two are considered as php script and executed by the server. Following is an exampl of "Hello World!" PHP script.

<?php
echo "Hello World!";
?>

Note! We can place our PHP code anywhere inside a .php file. Just remember to open and close the PHP code properly.

PHP inside HTML

We can also place our PHP code inside HTML. Following is an example of PHP code inside HTML.

<!DOCTYPE html>
<html>
  <head>
    <title>Index Page</title>
  </head>
  <body>
    <?php
      echo "Hello World!";
    ?>
  </body>
</html>

Save the above code in a .php file. So, when the file is requested the server will execute the PHP script and return the proper response to the browser.

PHP comments

Like most of the other languages like Java, C#, C++ etc. PHP also supports the two famous type of comments namely, single line comment and the multi line comment.

Single Line Comment

We use // to create single line comment. Following is an example of single line comment in PHP.

<?php
//this is a single line comment
?>

Multi Line Comment

We use /* */ to create multi line comment. Following is an example of multi line comment in PHP.

<?php
/**
 * This is a multi line
 * comment
 */
?>

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT