Tuesday, April 19, 2011

Random numbers in PHP

Do you think that I log in every day to update those figures and dates? Nope. Everything is done automatically. There’s a PHP function which returns the date that a file was last changed. So all I have to do is upload a new PDF file each time I make changes to the book.
A couple of lines of PHP code then produces the “Last updated…” section of the web page, automatically looking up and formatting the file’s date. Even the “NEW” marker is automatic. If the date of the PDF file is within 7 days of today’s date, the marker gets displayed. Otherwise, it doesn’t. So I never have to remember to remove the marker, because the web site does it for me.
As you can hopefully see, PHP isn’t just for creating databases. It’s great for managing and automating all aspects of a web site.

Random Numbers
Let’s imagine that, on a particular web page, we want to include a message on our home page which thanks visitors for looking at our site. However, to avoid bombarding people with the message, we only want to show it approximately one time in ten. So, every ten times that the web server sends out the page to a visitor, it will include the message.
One simple way to do this is with PHP’s random number generator. There’s a PHP function which returns a random integer between 1 and any number we specify. So we'll ask it to generate a number between 1 and 10, and only display the message if the number happens to be, say, 7. This will result in the message only being shown approximately 10% of the time.
That is to say, if 100 people visit the home page during one specific time period, roughly 10 of them will see the message.
Open PSPad and navigate to your demo.php file. Open it, delete everything that’s in it, and replace it with the following:
<HTML>
<body>
Welcome to our site.<br><br>
<?php
$r = rand(1,10);
if ($r == 7)
{
echo "Thank you for visiting<br>";
}
?>
</body>
</HTML>

Save the file (choose Save from the File menu), then open your web browser and surf to your demo.php file again. Keep pressing the Refresh button in your browser to reload the page. Most of the time you’ll see:



But if you keep refreshing the page (pressing F5 is usually the same as clicking the Refresh button, if you prefer), sometimes you’ll see this instead:



This is why PHP is so useful for generating dynamic HTML pages. It can generate different pages each time (or just some of the time). Take a look at the source code of those 2 HTML pages (use the View Source option in your browser). Note how there’s no PHP code in there, just the HTML that our code dynamically generated. The browser knows nothing about where the HTML code came from. It merely sent a "please send me the contents of demo.php" request to the server, and the server ran your program and sent some HTML back. The fact that the HTML was generated by a program, rather than being retrieved from a ready-made .html file on the server, is unknown to the browser.
Again, we’ve introduced a couple of new PHP concepts so let’s take another look at the code we just typed and uploaded. Here it is again, with line numbers to aid explanation:
 <HTML>
 <body>Welcome to our site.<br><br>
<?php
 $r = rand(1,10);
 if ($r == 7)
 {
 echo "Thank you for visiting<br>";
 }
 ?>
 </body>
 </HTML>

This time, I’ve chosen to mix HTML and PHP mode, and only to use PHP mode when necessary. So, to create our dynamically-generated HTML page, we start by outputting the necessary HTML and BODY tags. Remember that a PHP file always starts off in HTML mode, ie it’s just a standard HTML file as far as the web server is concerned. Just because it has a .php extension doesn’t change this. The only reason for the php extension is to tell the server that, if there is any PHP code in the file, it’s safe to run it. Some servers will also run PHP code that’s contained within a .HTM file, but many won’t.

And even if yours does, it’s a good habit to be able to identify, from its extension, whether a file is plain HTML or PHP. Line 3 generates the first line of text on the page, and I’ve added a couple of line break tags
too, for clarity. Once again, for clarity, I haven't created a css style sheet, or a doctype, or used <p> tags. The program will still work just fine, as browsers are generally pretty forgiving. When you're doing this for real, you'll need to do it properly.
Lines 11 and 12 close the HTML page neatly, by creating the closing body and HTML tags.

Line 4 flips the server into PHP mode. Line 5 creates a variable (we’ve called it $r) which uses the rand function. In this particular instance, the $r variable will be set to a value between 1 and 10. Note that every variable name in PHP has to be preceded with a dollar sign. Variables don’t have to be given single-letter names. I could just as easily have called it $my_random_value, but shorter is always best as there’s less chance of making a typing mistake when you refer to the same variable later on.

Line 6 checks to see whether $r is equal to 7. If it is, everything between the 2 curly brackets gets executed. If it’s not, everything between the curly brackets gets ignored.

Note two important points about the code in line 6. First, the if command is almost unique in PHP, in that it doesn’t (indeed, mustn’t) end with a semicolon. Second, when you’re comparing something, you use 2 "equals" signs rather than just one. So, $x = 99 will set the variable $x to 99. But to check whether $x is equal to 99 we say if ($x == 99). Note, too, how the value to check for has to go inside rounded brackets.
Incidentally, if you leave PHP mode and then re-enter it within the same file, no information is lost. So, for example, the following:
<html>
<?php
$name = "Fred";
?>
Your name is
<?php echo $name; ?>
</html>

Will display the message Your name is Fred.

0 comments:

Post a Comment

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Grants For Single Moms