PHP Syntax

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.

    //your php code goes here


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.

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

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.


// This is single line commented text, and
# This is another single line commented text

This is
a Multi-lines comment
block area


Subscribe Updates via Email

Join 49,000+ W3schools lovers and get all the latest tutorials, programs, algorithms in your inbox.