Chapter 2: What Is PHP? |
|
Some cool features of PHP | |
Generating random numbers: This makes creating page rotators and lots of other things easy. There are only two steps involved. First you "seed" the built-in random number generator to insure that it will be as random as possible. Then you ask for a random number from, say, 1 to 100. Here's the code: srand((double)time()*3.14159); This line "seeds" the generator. There's a lot going on here. Maybe you care how it works, maybe you don't. I'll explain just in case you want to know. The time( ) function returns the system time in seconds multiplied by our old friend "pi." The prefix (double) tells the computer to cast the result as a double-precision number. Casting means changing a variable's data type. The number that time() fetched is multiplied by 3.14159. It's then used as the "seed" value for srand(). There's any number of "strange" numbers you could pass to srand. For example, an md5 hash of some data item passed to your script. Never mind what that means; I'm just babbling in geek-speak. |
|
Note: As of PHP version 4.2.0, there is no need to seed the random number generator with srand() since this is now done automatically, according to the documentation at www.php.net. But you can still do it if you really want to... To generate a random number, call the rand(lowest,highest) function. Example: $number = rand(1, 100); This will pick some number between 1 and 100 and assign it to $number. Simple, right? What good are these random numbers? How about a random quote on your web site every day? Put your quotes in a file, read the file into an array, get a random number between 0 and the number of quotes you have and display the array element at that number (index). Surely you can think of lots more applications. Is there a catch? Well, sort of... You need to know how many quotes are in the file, so your random number can go high enough to display the last one. No biggie... use the count() function. count($array) returns the number of elements in $array. If there are 10 quotes in the file, it returns 10. But the indexes go from 0-9; there's no $array[10] - and $array[1] isn't the first element. Problem? Nahhh. You could just start at 0 and handle 10 as an exception. Easy as pie. There are simpler ways but this will work. Here's an example: $index = rand(0, count($array)); Convert a string to an array: Why do that? "We like ... explosions ... that leave you feeling good." - Devo Sorry, my mind was wandering... Sometimes you need to break a line up into "words." The nifty explode() function does this easily. Just figure out what character separates the "words" in the string. Maybe it's a space. Then call explode() on the string. $array = explode(" ", $string); OR $array = explode($delimiter, $string); where $delimiter is the character or characters that separate the "words" in the string. Why do I keep saying "words" in quotation marks? They might not be normal words in any human language. They might be some other chunks of data that need splitting up. Convert an array to a string: It should be no surprise that implode() is the opposite of explode( ). And, yes, it's built in, too. Suppose you had this array: $array = ("This", "is", "a", "sentence."); You can turn the list into a string like this: $sentence = implode(" ", $array); When implode is finished, you'll have: $sentence = "This is a sentence"; because you stuck the words together using a space as a separator. |
|
|
|
Sorting: Sometimes you want things sorted. PHP has a slick little function for that, too. It's called sort(unsorted_array). It sorts strings in alphabetical order and sorts numbers in numerical order. Sorted items will be ordered from lowest to highest. If you need to sort stuff in reverse order, then use rsort(unsorted_array). Tip: Suppose you have a bunch of quotes for your random quote generator in a file. Or perhaps a list of books, recipes, etc. Each time you add an item to the file, it goes at the end. You could keep it in order using these built-in functions and a few lines of code. First, add the new item. Then read the file into an array. Sort
the array and save it all back to a file. You could make a little web page with a form for this.
Enter your new quote on the form; the form calls a script to re-sort the file and you're done. | |
|
| Previous Page Table of Contents Next Page |
Copyright © 2004 Steve Humphrey |