Your First PHP Programs
PHP is an interpreted scripting language, meaning that the language engine simply runs the scripts you have written with no necessary intermediate steps to compile or convert them into binary formats. Most of the scripts that will make up your web applications go in the same place as your HTML content files. Files containing PHP script will typically have a .php extension, although many people still use the older extensions.php3 and .phtml. Where you place these files will depend on how you have configured your web site and what extension the web server will use to pass the file to the PHP interpreter. We will refer to this location as the document root.
When the web server receives a request for an appropriately named file, it passes this to the PHP engine, which then processes the file one line at a time. For lines that do not contain PHP code (typically, lines containing HTML markup), the server emits them untouched and executes any PHP script code as it is seen. With this in mind, we write our first program, which is a mixture of PHP and HTML.
<html> <head> <title>My First PHP Program</title> </head> <body> <?php echo "Hello Everybody!"; ?> </body> </html>
If we type this into a file called firstprogram.php and put it in our document root directory, we can then type the URL for the path to this file (for example, http://localhost/gettingstarted/firstprogram.php). We should then see something similar to Figure 1-1.
Figure 1-1. Your first PHP program in action.
<?php ... ?>
When the PHP language engine sees these two tags, it processes the script and takes any appropriate actions. When the script portion ends, it resumes output of HTML.
<?php phpinfo(); ?>
This script produces output similar to that seen in Figure 1-2. (We did not have to use HTML headers or markup since the phpinfo function emits these for us.)
Figure 1-2. Learning about our PHP interpreter with the phpinfo() function.