Online Book Reader

Home Category

Running Linux, 5th Edition - Matthias Kalle Dalheimer [454]

By Root 1520 0
(we leave out the HTML framework here for brevity):

echo "Dear friends, today's date is: ";

echo date("F d, Y")."\n";

echo "
";

echo "We are in the ";

if ( date ("m") <= 6 ) {

echo "first ";

} else {

echo "second ";

}

echo "half of the year ".date("Y");

?>

You have probably already guessed that this script makes its decision in the if statement depending on the current month. Notice that we have used an HTML tag (
) in the PHP output; this is completely acceptable and a very common technique when using PHP. Your web browser will receive the following data (of course, with other dates, unless your computer clock is wrong or you were trapped in a time warp):

Dear friends, today's date is: May 04, 2002


We are in the first half of the year of 2002

The web browser will then break the line at the position of the
tab.

In order to modularize your code, PHP also supports functions, as do most other programming languages. These functions enable you to execute a piece of code in many different places without having to rewrite it over and over again.

* * *

Tip


PHP comes with very extensive function libraries, and you can download even more from the Net. To include a function library, you can use the include() and the require() statements, which differ only marginally.

If you want to program with PHP, you should familiarize yourself with the documentation of the function libraries that are shipped together with the PHP interpreter, since their use means you do not have to reinvent the wheel when performing common tasks.

* * *

Here is the definition of two simple functions--show_date, which outputs the current date in a hardcoded date format and appends a line break, and show_halfyear, which outputs first or second depending on the current month:

function show_date() {

echo date("F d, Y") . "\n
";

}

function show_halfyear() {

if (date("m") <= 6) {

echo "first ";

} else {

echo "second ";

}

}

?>

Let's call this script functions.php and rewrite our initial script using these functions:

require("functions.php");

echo "Dear friends, the date today is: ";

show_date();

echo "
";

echo "We are in the ";

show_semester();

echo "semester of " . date("Y");

?>

The require() statement tells the PHP interpreter to load our function script and make the functions contained therein available to the current script.

Of course, we have only scratched the surface of what PHP can do. If this has whetted your appetite, you might want to look into Programming PHP, by Rasmus Lerdorf, the original author of PHP, and Kevin Tatroe (O'Reilly).

Until PHP3, PHP was an interpreted language, the code of which was kept in a buffer. Loops and other often-run pieces of code were parsed over and over again before executing the code. Of course, this led to suboptimal performance.

PHP4 is a complete rewrite and consists of the language core (called "Zend") and the function modules (which are very flexible and extensible). Unlike PHP3, PHP4 can be used in multithreaded environments, which also makes it possible to use PHP as a module in various web servers. PHP5 was yet another rewrite (using the "Zend2" engine) with a new, more intuitive object model, improved performance, and support for exceptions. http://www.php.net/manual/en/migration5.php gives you the scoop on the differences between PHP4 and PHP5.

Besides PHP itself, it may be a good idea to download and install phpMyAdmin. phpMyAdmin is a database administration tool written in PHP that simplifies your daily work when it comes to the administration of MySQL, and handles tasks such as creating and dropping databases and tables. It also can manage privileges, keys and fields, and even more. Besides, it is an excellent example of source code that accesses MySQL databases from PHP code! You can download phpMyAdmin from http://www.phpmyadmin.net. When you have installed it according to the instructions, you can open the URL http://localhost/phpMyAdmin in your browser, and you will see the screen

Return Main Page Previous Page Next Page

®Online Book Reader