Again, PHP3.0 commands can be easily inserted alongside HTML. This provides an easy way to incorporate dynamic information within what was previously a static document. For example, if we would like to insert the current date into the HTML document that visitor has called, we might do the following:
<TITLE>Our first PHP3.0 script</TITLE>
<CENTER>Our first PHP3.0 script</CENTER>
/* the above "<?" signals that the PHP script has begun */
$today = date("Y-m-d");
PRINT "<CENTER>Today is: $today.</CENTER>";
# the following "?>" closes the script
Assuming today is August 27, 1998, our output would be:
Our first PHP3.0 script
Today is: 1998-08-27.
A few points to make:
All PHP3.0 commands must be enclosed within the "<?" and "?>" brackets. A second method to denote PHP3.0 commands is by enclosing them within "<?php...?>" brackets.
Comments can be made within the script, and are enclosed within "/*" and "*/" brackets, or by placing a "#" sign at the beginning of the line.
All statements that are to be outputted to the screen must be enclosed in double quotations ("), and led by the PRINT statement.
Almost every PHP3.0 command must end in a semi-colon (;).
Any HTML commands placed within the PRINT statements will be interpreted by the browser, and perform their usual actions.
Documents including PHP3.0 statements must be saved with the extension *.php3 (For example, myphpfile.php3). This tells the PHP3.0 interpreter to execute any commands found within the script.
In short, the date command operates as follows: Syntax: string date(string format, int timestamp);
The function can take two variables (timestamp) is optional. If supplied with a timestamp, the function returns a string containing a date formatted according to the parameters within the format string. In the above example, Y-m-d signifies year, month, and day, respectively, all in numerical format. There are many other format characters that can be placed within the format string. For a complete list, click here.
The include statement Another very powerful use of PHP3.0 is the capability of building HTML templates, which are very useful when one is developing a cohesive site with many pages. Let's assume one would like to place a footer at the bottom of each page, for example:
But what happens when the site increases in size to say 50 pages, and suddenly the footer must include another statement? This would involve the modification of each page, one by one, taking up a lot of time and effort.
However, PHP3.0 offers the include statement, which allows one to include a separate file within an HTML document. Let's insert the above copyright statement into a separate text file, and save it as "footer.txt". Then, one only has to insert the following command at the bottom of the HTML file:
and the footer will appear when loaded into the browser! Supposing the
site grows to 50 pages, and that copyright needs to be modified. The only step the developer has to take is to open the footer file, make the necessary modification, and Voila!. 50 pages are immediately updated.
Interesting, huh? Now, let's look at some of the more dynamic aspects of the language, incorporating HTML forms and variables passing/modification.