We'll start with two quick examples. Create a new directory for PHP work and cd into it: $ mkdir /var/www/html/php $ chmod a+rx /var/www/html/php $ cd /var/www/html/php Create a PHP file named hello.php : [4]
<html> <head> <title>Hello, world! with PHP</title> </head> <body bgcolor="#ffffff"> <? echo "hello, world!" ?> </body> </html> To view the result of this file, load either one of these URLs: http://localhost/php/hello.php or www.opensourcewebbook.com/php/hello.php. The result can be seen in Figure 12.1. Figure 12.1. hello, world! with PHP
The PHP code is embedded in the HTML within the <? ... ?> tag. In this example the PHP code simply echoes (displays) the string "hello, world!". The echo PHP statement outputs a string that eventually is rendered in the browser. Now that the obligatory "hello, world!" example has been done with PHP, try a built-in function that does a lot of work: phpinfo() . Place this code into phpinfo.php : <html> <head> <title>PHP Information</title> </head> <body bgcolor="#ffffff"> <? phpinfo() ?> </body> </html> The function phpinfo() builds a page with a wealth of useful information on how PHP was built, the PHP environment, etc. To view the result, load either of these URLs: http://localhost/php/phpinfo.php or www.opensourcewebbook.com/php/phpinfo.php. The result of this can be seen in Figure 12.2. Figure 12.2. PHP information with phpinfo()
|