Getting Started with PHP : Writing Hello World Program

Lets start this section with small example, consider a small portion of PHP code

<?php
	$var = 5;
	echo "the value of variable is ". $var;
?>

This program simply outputs a string “the value of variable is 5” on a browser. Now lets see the mechanism how it works.

 

Every PHP code segment must start with <?php tag and end with ?> tag. Note that there is no space between < , ? and php. If web server finds <?php ?> tag on a web page that browser is requesting, then it converts all the PHP statement into corresponding HTML statement which browser can understand. If you run the above code on web server and view the source, then you see that there are no any php statements.

 

Every PHP variable declaration start with $ sign. PHP is not so strict like programming languages therefore all types of variable (int, float, string, long etc) can be declared by just writing $var_name. For example:

?php
    $intvar = 5;
    $floatvar = 5.5;
    $stringvar = "nepal";
?>

To print a statement to a browser window, PHP command echo is used. The statement to be printed is kept inside single or double quotes. The . (dot) operator concatenates two string.

Embedding PHP code with HTML

PHP is designed to use with HTML. PHP alone cannot do anything. Any website needs different HTML tags. But PHP with HTML helps to create dynamic pages. So PHP code can be embedded inside HTML code as below.

<HTML>
<HEAD>
<TITLE>My First Program</TITLE>
<HEAD>
<BODY>
<?php
    $var = 5;
    echo "the value of variable is " . $var;
?>
</BODY>
</HTML>
SHARE Getting Started with PHP : Writing Hello World Program

You may also like...

Leave a Reply

Your email address will not be published.

Share