Course Banner


PHP Syntax

PHP syntax matches very much from ‘C’ programing language. If you know ‘C’ language, you will find yourself familiar with PHP Coding. Even you do not know ‘C’ programing language, nothing to worry about. PHP Syntax is more of English like and it’s really very easy to grasp.

Here is the basic syntax of PHP:


<?php

echo"Hello World";

?>

As you can see in the above code, PHP starts with angular bracket < with question mark followed by the word ‘php’. <?php and ends with  question mark and then angular bracket >.

Between these codes you can write your php program.

In the above program case, we have just printed a familiar ‘Hello World’ line.

Since it’s an string, we have enclosed it with double quotation marks. We can use single quotation also. And at the end, we have user sentence terminator semicolon(;)

Save the file inside C/xampp/htdocs and then create a folder called PHP-BOOK and then save the file inside that.

Note that the location of the file and xampp may vary if you have installed xampp in different drive.

In my case, I have installed xampp in ‘C’ drive.

To see the output open your web browser and then type following:

http://localhost/PHP-BOOK/hello-world.php

And you can see this output

Here ‘localhost’is called virtual path. It’s called virtual path because this path/folder does not exist into our computer. Its equivalent to http://localhost/ i.e.  c:/xampp/htdocs. Apache server maps local path with virtual path.

 

Let’s have a look at another program.


<?php

 

$f_name ="Umar";

$l_name ="Rahman";

 

echo$f_name;

echo$l_name;

 

?>

Output :

In the above program, we have declared two variables,$_fname and $l_name. PHP variables start with ‘$’ sign followed by the variable name. You will know more about variables in next chapter.

You can also declare third variable say $full_name and join together f_name and l_name.


<?php

 

$f_name ="Umar";

$l_name ="Rahman";

 

$full_name = $f_name. " " .$l_name;

 

echo$full_name;

 

?>

$full_name = $f_name. " " .$l_name;

 

Look at above syntax. We have assigned $f_name and $l_name variables into a single variable called $full_name.

Also, we have used  dot(.) operator. Dot operator acts as an glue to put together two variables or strings.

We have used “ ” for space between first name and last name.