Chapter 2: What Is PHP?

Google

Some cool features of PHP

But what happens if you do this when you're creating an array?

$fruits[0] = "Peach";
$fruits[2] = "Pear";
$fruits[4] = "Melon";

If you thought the array would be messed up, you'd be wrong. PHP just creates the elements $fruits[1] and $fruits[3] anyway and leaves the values empty. How cool is that?

Tip: You can add elements to an array inside a loop by writing a line like this:

$monthName[$index] = $value;

Each time the loop runs, the next element will be at position $index and will have the value of $value.

Associative arrays or hashes: PHP also has arrays that can be indexed by strings instead of numbers. In fact, the index can be a number or a string of characters that may or may not include numbers. What the heck would that look like? Here are some examples:

$monthName = array("Jan"=>"January", "Feb"=>"February","Mar"=>"March",
"Apr"=>"April", "May"=>"May", "Jun"=>"June", "Jul"="July", "Aug"=>"August",
"Sep"=>"September", "Oct"=>"October", "Nov"=>"November", "Dec"=>"December");

$monthName["Jan"] = "January";
$monthName["Feb"] = "February";
$monthName["Mar"] = "March";

Using an associative array, you could even make a list of the months of the year indexed by number AND abbreviation. Then, you could look up January in the array as either $monthName[1] or $monthName[Jan]. Just put all the elements indexed by numbers and all of them indexed by abbreviations into the same array.

Accessing array elements: An array element is located by the name of the array and its index. Most of the time, what you're doing is getting something from an array and copying (assigning) it to a variable. You might need to display it, modify it, etc. Here's two typical assignment statements of that kind:

$month = $monthName[1];
$month = $monthName[Feb];

Reading a file into an array: Hmmm...why would you want to do that? Suppose you were using a text file as a database. Each line of the file would be one database record. The fields would be separated by a pipe (|) or some other character(s) so that you could pull out a record and split out the fields. If you made the first record a line that contained the field names, some other nice things would happen. More on that later.

PHP has a built-in function called file() to do this job. Suppose your text file was named visitors.txt and contained these lines:

ip|date|browser|os|language
64.36.44.124|0425|MSIE 6.0|Windows NT 5.0|en_US.UTF-8
222.5.64.79|0725|MSIE 6.0|Windows NT 5.0|en_US.UTF-8
237.48.13.58|0911|MSIE 6.0|Windows NT 5.0|en_US.UTF-8
43.27.99.2|1031|MSIE 6.0|Windows NT 4.0|en_US.UTF-8
125.5.6.104|1225|MSIE 5.5|Windows NT 5.0|en_US.UTF-8

To get these lines into an array, just call file() on the filename:

$array = file("partners.dat");

Pretty printing: Want to see what the array actually looks like? Use the print_r() function. It takes an array and creates a readable display from it. Copy these three lines into a new file and call it showMe.php (or whatever you like):

header("Content-Type: text.plain");
$array = file("partners.dat");
print_r($array);

If you upload both files (showMe.php and partners.dat) to your server and run showMe.php, you'll get a web page with the array definition in plain text:

Array
{
[0] => ip|date|browser|os|language

[1] => 222.5.64.79|0725|MSIE 6.0|Windows NT 5.0|en_US.UTF-8

[2] => 237.48.13.58|0911|MSIE 6.0|Windows NT 5.0|en_US.UTF-8

[3] => 43.27.99.2|1031|MSIE 6.0|Windows NT 4.0|en_US.UTF-8

[4] => 43.27.99.2|1031|MSIE 6.0|Windows NT 4.0|en_US.UTF-8

[5] => 125.5.6.104|1225|MSIE 5.5|Windows NT 5.0|en_US.UTF-8
}

Nice and readable ... but what's the deal with all those blank lines? For one thing, print_r() puts in a linefeed after each array element. For another, each of those lines in the file had a linefeed at the end. So, you get double-spaced text.

Want to get rid of the blank lines? Just loop through the array and use the trim() function on each line. It removes whitespace (tabs, spaces and other characters that aren't displayed on your screen) at the start and end of whatever string you feed to it. How do you do that? Like this:

foreach($array as $k=>$v) { $array[$v] = trim($array[$v]); }

Previous Page   Table of Contents   Next Page

Copyright © 2004 Steve Humphrey