This lesson has a detailed description of PHP Syntax. It's essential for you to before proceeding to learn more advanced lessons in PHP.

PHP Tags

PHP is designed to work with HTML, so you can easily write and embed PHP code with HTML. A PHP code block begins with <?php tag and ends with ?> tag.

Syntax:

<?php
    //your php code goes here
?>

PHP with HTML

echo is a command in PHP for writing output data to the browser, and at the end of each PHP statement we require to write a ; (semicolon) as you see in the below example code. Otherwise, if you write another statement, then PHP will report a syntax error.

Example:

<html>
<title>Hello World program in PHP</title>
<body>
<?php echo "Hello World"; ?>
</body>
</html>

To run this PHP program, you need to write above code in any text editor and save it to your web server under www directory with a .php extension.

Once it's done, start the server and go to localhost and type in the path of your file, i.e., http://localhost/HelloWorld.php

Program Output:

How to write and execute PHP

Comments in PHP

The PHP interpreter ignores the comment block; it can be used anywhere in the program to add info about program or code block, which can be helpful for the programmer to understand the code easily in the feature.

In PHP, we can use // or # to make a single-line comment, and /* and */ to make a large comment block.

Example:

<html>
<body>

<?php
// This is single line commented text, and
# This is another single line commented text

/*
This is
a Multi-lines comment
block area
*/
?>

</body>
</html>