PHP Basic Syntax

Unlike HTML, CSS and JavaScript whose scripts are executed on the browser, PHP script is executed on the Server and the result is sent back on the web browser. PHP is an open-source programming language, that is widely used to build dynamic websites. All PHP files are saved with an extension of .php.

Where to Create PHP Files

As discussed in our previous post, you can’t create PHP files anywhere in the system, like you did where creating HTML, CSS and JavaScript files. PHP files must be created inside the specific folder in our XAMPP. So open a XAMPP server and search for a folder namely htdocs.

Double click on this folder and inside it create another folder, you can name it anything you want. Let me name it PHPtest.

Once it’s done, now inside this PHPtest folder you can create your PHP files and save it with an extension of .php.

Now you need to open that PHP file that you have created inside the PHPtest folder on your code editor and start writing a PHP code.

Write Hello World in PHP

PHP scripts always starts with <?php and ends with ?>. These tags are called canonical tags. Between these canonical tags you can write all your PHP code. Here is a Syntax:


// PHP code here...;

To understand it clearly let’s write our first PHP program and print Hello World.


   echo "Hello World!";


Hello World!

The echo word is used to print any text in PHP. Like we use document.write in JavaScript and print () in Python. Also remember that PHP code always ends with semicolon ;.

You can run this code on any code editor if you have installed PHP extension. In case, if you don’t have installed PHP Extension or your Code Editor doesn’t have any PHP extension you can open the web browser and type localhost/phpFolderName. phpFolderName is actually the folder where you have store your php file.

Keep in mind that you must open Apache and MySQL component, before you run your PHP Code. As discussed in our Previous Post.

Comments in PHP

Comments in PHP or in any other programming language are used to provide extra information about the code. It helps other programmers to clearly understand the code. Comments are a sequence of characters that is not executed whenever we run the code and they do not effect the execution of our script. Here is how you can add comments in PHP.

// This is a single line comment.
/* This is a 
multi line
comment */

In PHP, // is used to add single line comment. But if want to add comment that fits more the one line then we must enclose it within /*…*/.

Share This Post!

PHP Basic Syntax

Leave a Reply

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