2009-09-01
| Table of Contents: |
| Rate This Article: | Add This Article To: |
( Page 2 of 5 )
Dynamic Pages
In the previous example, every time you open the .php file in the browser, you’ll see the same thing. That’s not particularly useful, as you’re not really taking advantage of the power of PHP. PHP is a powerful, complete programming language with which you can create variables, functions, and even classes. Your PHP code can open files, connect to remote web servers, make database connections, and so on. (Always remember: The PHP code is running on the Web server, not inside the browser. So a PHP program that opens up a database connection is doing so on the server. The browser is not opening the database connection.) And the results of your PHP program are sent back down to the web browser usually in the form of HTML, which the browser can display.
Here’s a (slightly) more interesting example. PHP includes a function that gives you a random integer. If you put this function in a PHP file, the function will get called each time a person using the web browser accesses the web page. So if you click refresh, you’ll see a different number. Here’s the .php file; save it on your web server with a .php extension, such as rand.php:
<html>
<body>
A random number:
<?php
echo(rand());
?>
</body>
</html>
Figure 2 shows what happens when I open the page in the browser:

Figure 2
The number 13817 is random and was generated by the rand function in the PHP program that was running on the Web server. If I refresh the page, the browser sends another request to the web server; the web server then asks the PHP processor to process the page again. Once again, the PHP processors reads in the page and runs the PHP code. The rand function runs once again, and this time generates a different random number. Because I'm using the echo function, the random number is placed inside the HTML, which is, in turn, sent back down to the browser.
When I refresh the page, I see a different number, as shown in Figure 3.

Figure 3
![]() |
|


