PHP BASICS

 

So what are those thingys anyways?

PHP is an "Interpreted" language. This means that PHP is NOT compiled, it is parsed at runtime (the time when the script is run. i.e. user visits the page).

All PHP code is enclosed within "". Functions (text that causes action in the script), are ended by the semicolon (";"). Comments (or escaped text) are as follows:

#shell style comment
//comment
/* C-style comments */

Ok, so what exactly does a PHP page look like?


<?php
$name ="Jehanzeb Manzoor";
echo"<html><head><title>Look!ItsPHP!</title></head>";
echo"<body>";
echo"Hi,mynameis,$name,daremetodrive?";
echo"</body></html>";
?>

Now lets take a look at this. First, we initialize the PHP interpreter. On the second line, we assign the variable "$name" to the value "Jehanzeb Manzoor". This is done by first giving the variable a name.

All variables (with the exception of constants, which you need not to worry about) start with the dollar sign ("$"). After we give the variable a name, we assign it a value, with the Assignment Operator ("="). The value can be enclosed in either double or single quotes. Use double when you want to invoke"Variable Interpolation". This means that if a variable is found in between " and ", the value will be printed. But, if you use single quotes, the values will not be printed, instead, the variable name will be printed.

Example:


<?php
$variable ="Iamavariable";
echo"Textfromvariable:$variable";
//willprint:Textfromvariable:Iamavariable;
echo'Textfromvariable:$variable';
//willprint:Textfromvariable:$variable
?>
Next, we use the echo function. There are many ways to display text in PHP, echo is the most common. We do four lines of the echo statement to demonstrate how PHP can be integrated with HTML. The first line prints the head and title information, the second line starts the body tag, the third line prints the text "Hi, my name is, Jehanzeb Manzoor, dare me to drive?", and the fourth line closed the HTML. Finally, we end the script, closing the PHP tag.

Conclusion:

Today you've learned a little of what PHP can do. Now you understand what a sample PHP page looks like, and how one works. You have learned about variables, the echo statement, and variable interpolation.

For further learning, I suggest you go out, and write your own version. Play around with variables, the echo statement, and variable interpolation.

 

Hosted by www.Geocities.ws

1