PHP Tutorials Lesson 3
Introduction:
In this tutorial you will learn
1. Arrays
2. File Functions
3. Randomizing
4. Putting It Together
By the end you should be able to display random quotes, combine this with the previous tutorials and you should be able to make a form which adds a quote to a text file which then displays a random quote from that file.
Arrays:
An array is basically a list of something, that is numbered. This would be an array called dogs.
Dogs:
0. Golden Retriever
1. Rottweiler
2. Labrador Retriever
Notice that it starts at zero? In pretty much all languages arrays start at zero.
In php this would be
Code:
$dog = array("Golden Retriever", "Rottweiler", "Labrador Retriever");
//Now to display it we would use $dog[index] like so
echo $dog[0]; //shows Golden Retriever
echo $dog[1]; //shows Rottweiler
echo $dog[2]; //shows Labrador Retriever
You could also put variables in an array.
There is also another kind of array, say you don't want to remember what number it is, instead remember a keyword. This will also come in useful when you start in mysql.
Code:
$dog = array(
"Golden Retriever" => "Sammy",
"Rottweiler" => "Spot",
"Labrador Retriever" => "Snickers",
);
echo "My Golden Retrievers name is $dog['Golden Retriever'], my Rottweilers name is $dog['Rottweiler'], and my Labrador Retrievers name is $dog['Labrador Retriever']";
//that will show My Golden Retrievers name is Sammy, my Rottweilers name is Spot, and my Labrador Retrievers name is Snickers.
Some things to notice is the =>'s, im not sure if it was intended or not but they look sorta like arrows. Notice that the array can be set up multi-lined as well, as long as you open it with parenthesis and that you end it with a semi-colon. One important thing is that each line is seperated by a comma, not a semi-colon. The final part of arrays is multidimension arrays, it is somewhat like an array within an array. Take a look at this code:
Code:
$dog = array(
"Golden Retriever" => array("Sammy", "Fast"),
"Rottweiler" => array("Spot", "Lazy"),
"Labrador Retriever" => array("Snickers", "Slow"),
);
echo "My Golden Retriever's name is $dog['Golden Retriever'][0] and is $dog['Golden Retriever'][1], my Rottweiler's name is $dog['Rottweiler'][0] and is $dog['Rottweiler'][1], my Labrador Retriever's name is $dog['Labrador Retriever'][0] and is $dog['Labrador Retriever'][1].;
//That will show My Golden Retriever's name is Sammy and is Fast, my Rottweiler's name is Spot and is Lazy, my Labrador Retriever's name is Snickers and is Slow.
Now this information about arrays wasn't totally necessary for this tutorial, but instead of leaving you copying/pasting I decided to write it out so you'd know.
File Functions:
For this part we will use the following functions
* File
* Fopen
* Fwrite
* Fclose
File:
What file does is open up a file and put the info into an array seperated by each line. Lets take a look at this code
Code:
$quotes = file("quotes.txt");
In quotes.txt you have
File is automatically doing this
Code:
$file = array("This is a quote", "This is another quote", "Quote 3");
The syntax is
file(page);
NOTE: You do not need to use fopen when using file
Fopen:
As you may guess, this function opens a file for appending, writing, etc. The format is
$variable = fopen(page, mode);
$variable is because when you use other functions to write to it, etc, you have to reference back to the fopen. $variable is your reference point. Page is just what it says, the page you want to open. Mode is what you want to do with it, here are the modes that you need to know right now
Fwrite:
This writes to the file, obviously

:. The syntax is
fwrite(variable, "what_you_want_to_write");
Remember I said you needed a variable in Fopen? That will be used here to tell the server what file to write too. Depending on your mode in Fopen it will either append, read, read/write, etc.
Fclose:
Fclose does just that, close the file to prevent corruption. Syntax is
fclose(variable);
Yet again using that reference point that I told you about when using fopen.
Randomizing:
For this part we will use the following functions
*
Count
*
Rand
Count:
Count counts the amount of things in a variable, if you want it to count the number of items in an array you could use a code like this
Code:
$dogs = array("Golden Retriever", "Rottweiler", "Labrador Retriever");
$count = count($dogs);
echo $count;
//will display 3
Notice that it's not starting at 0 like arrays do. Syntax is
count(item);
Rand:
Produces a random number between a given maximum and minimum integer, or a completely random number. Sample code:
Code:
echo rand() . "\n"; //Could display something like 2042
echo echo rand(1, 10); //Could display something like 5
NOTE: When creating a completely random number it actually creates a number between 0 and RAND_MAX. On some servers RAND_MAX is 32768. If you need something higher you will need to specify a maximum number higher then that.
Putting It All Together
To create our quotes system you first have to take your quotes and put them in an array
Code:
$quotes = file("quotes.txt");
Now you must randomly select a quote from that array, remember how it was sorted by numbers?
Code:
$quote = $quotes[rand(0, count($quotes) - 1)];
Lets look at this part for a bit
Code:
rand(0, count($quotes) - 1;
You are creating a random number with a starting point of zero and an ending point of the number of quotes minus one. Minus one because count starts with one and arrays start with zero, so in the array there is no quote four (considering you have an array of three).
So now your code is saying $quote = $quotes[random_number], just have it echo it and your done! Our full code is
Code:
<?php
$quotes = file("quotes.txt");
$quote = $quotes[rand(0, count($quotes) - 1)];
echo $quote;
?>
Conclusion:
Hopefully you understand everything i've said so far. Using the fwrite function you should be able to write to quotes.txt, however remember that after each one you have to have a line break. Happy coding
