Open Source Web Development with LAMP Using Linux, Apache, MySQL, Perl, and PHP [Electronic resources]

James Lee, Brent Ware

نسخه متنی -صفحه : 136/ 129
نمايش فراداده

12.4 A Couple of Quick Examples

We'll start with two quick examples. Create a new directory for PHP work and cd into it:

$ mkdir /var/wwwl/php 
$ chmod a+rx /var/wwwl/php 
$ cd /var/wwwl/php 

Create a PHP file named hello.php: [4]

[4] Remember the permissions, 644.

<l> 
<head> 
<title>Hello, world! with PHP</title> 
</head> 
<body bgcolor="#ffffff"> 
<? echo "hello, world!" ?> 
</body> 
<l> 

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:

<l> 
<head> 
<title>PHP Information</title> 
</head> 
<body bgcolor="#ffffff"> 
<? phpinfo() ?> 
</body> 
<l> 

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()