Introduction:
I spend a lot of time in forums, and the question of "Where to start?" arises commonly. So I decided to go off and write one somewhere. My objective is to get you familiar with PHP, without overloading you with unnecessary "techonobabble".
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:
PHP Code:
#shell style comment
//comment
/* C-style comments */
Ok, so what exactly does a PHP page look like?
PHP Code:
<?php
="Jaguar XK8";
echo"<html><head><title>Look!ItsPHP!</title></head>";
echo"<body>";
echo"Hi,mynameis,,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 "Jerome Gagner". 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 Code:
<?php
="Iamavariable";
echo"Textfromvariable:";
//willprint:Textfromvariable:Iamavariable;
echo'Textfromvariable:';
//willprint:Textfromvariable:
?>
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, Jaguar XK8, dare to drive me?", and the fourth line closed the HTML. Finally, we end the script, closing the PHP tag.