|PHP: Variables, Arrays, and Functions (Don't Say It)|
|Programming - Web Languages|
|Written by David Shirey|
|Wednesday, 17 July 2013 00:00|
You've used arrays in RPG, but they're so much more important in PHP.
Last May, we talked about variables and how they're used in PHP. If you were paying any attention, which I wasn't, you know that PHP supports the following variable types: string (alphanumeric), integer, floating point (decimals), Boolean (true or false), array, object, null, and resource. Now we want to zero in on one of those variable types that we talked about—namely, arrays—and look at how they're used.
You know what would be easy? To just read through this article and think, "Aw, that ain't so bad." But you would learn absolutely nothing that way. I want to encourage you to get WAMP or MAMP (or some sort of Web server) going on your machine, grab a text editor, and use the code samples in here to get some actual practice. Just remember that every script should start with <?php and end with ?>. And be sure to save your text files as .php.
RPG has arrays, but the functionality in PHP for the array variable type is more robust than what RPG has, and you will have difficulty finding many PHP scripts that do not have a number of arrays in them. In general, there are two types of arrays in the PHP world: numeric and associative.
The good news is that with one big yet small difference, numeric arrays in PHP are very similar to the kinds of arrays we RPG people are used to.
The word numeric refers not to the data kept in the array (that can be numeric or string) but rather to the subscript or index that's used to identify and access individual entries in the array. In a numeric array, they're…duh…numeric.
The primary difference between an RPG and a PHP array is that while in RPG the subscript starts with 1 (that is, the first element in an RPG array is indexed as 1), the first element in a PHP array is the 0 element. I know; it's crazy. But that's the way C does things, and when you get right down to it, PHP is written in C, so what can you do? If your parents are nuts, chances are you're going to be nuts too.
Creating an array is simple; just indicate the name and call it an array. There's no need to define the size of the array, the number of elements in it, or anything else when you create it; PHP will dynamically add space as needed.
$MyArray = array()
Look familiar? It should. Remember, an array is just a variable, so creating an array looks just like creating a variable except that, rather than defining it by moving a value to it, we define it by just declaring it to be an array.
The easiest way to put values in this array is to let PHP do the indexing for you. That is, PHP is able to see what the last index value used was and assign the next value to the next index value. To do this, simply use brackets instead of parentheses and don't enter an index value. For example:
$MyArray = 'Dave';
$MyArray = 'David';
$MyArray = 'Davey';
This will result (if this is the first time you're putting a value in this array) in the zero index spot being assigned the value 'Dave', the 1 index holds 'David', and lucky index 2 has 'Davey'.
You'll also use brackets if you're picking up a value from the array. The following example sets the variable $value to be equal to the 11th entry in the array.
$value = $MyArray
All in all, numeric arrays are pretty straightforward.
But PHP doesn't stop there. The second type of array that you can define is the associative array. Here, the index or subscript isn't a number, but rather a string. What's the point of that? Well, since you're defining the string value (rather than letting it default to the set of all integers starting with zero), you can assign values that actually have meaning—things like 'first name', 'last name', 'street address', etc.
That is, we could define an array that represents an address, with elements or indexes that represent the different parts of the address. And by defining it as an array, we can then move it as a unit rather than having to necessarily deal with the individual data elements.
$MyArray = array(
'name' => 'John Smith',
'address' => '1304 Sherwood Ave',
'city' => 'Your City',
'state' => 'XX',
'zip code' => '99999-9999'
In the above code, you have two "things"; for example, the 'name' and 'John Smith' connected by a => thingy. The 'name' is the property; this is the entity you're defining. The 'John Smith' is the value of the property. We'll see this below when we do a "foreach" to display the contents of the array. Also notice that there's only one semicolon (at the end of the whole thing), but there's a comma after every property/value pair except the last one.
One of the things that can be really confusing about PHP is the many number of ways that the same action can be coded. This is partly because PHP is free-form and partly because it's a Web language, and that means anything goes. So the array can also be set up horizontally. There's no real difference in the syntax; it just really depends on how you want to see things, what looks more intuitive.
$MyArray = array('name'=>'John Smith', 'address'=>'1304 Sherwood Ave',
'city'=>'Your City', 'state'=>'XX', 'zip code'=>'99999-9999');
$MyArray = array();
$MyArray['name'] = 'John Smith';
$MyArray['address'] = '1304 Sherwood Ave';
What's important to notice is that, in the first two formats, you use => to associate a property with a value. But in the third format, you use =. That's not a misprint; that's the way it is. I know. Confusing. But remember that when we set the numeric array earlier and we assigned values using the empty brackets, we also used an = sign there, so it does sort of make sense. Also, the first two have a semicolon only after the whole mess, but the last one, which is breaking it up into separate PHP statements, has a semicolon after each line.
Two-Dimensional Associative Arrays
The associative array above is neat, but it's really just a one-dimensional array. This is great if you have just one address or if all of the properties are the same type (like the names of cities or cars), but what if you have a more complex situation? You could use several arrays, of course, and that might make sense, but in the end we want to be able to create a multi-dimensional array where we can load multiple records.
Fortunately, defining two or three (or whatever) dimensional arrays in PHP is easy if not tedious. Using one of our formats from above, we expand it slightly.
$MyArray = array(array('name'=>'Dave', 'dob'=>'1985-05-15'),
We have now loaded a two-dimensional array that, in structure, looks strangely like a set of file records from RPG. Any time you're bringing data in from a subfile-like PHP scripted page (even though PHP never uses the term "subfiles") or reading records from a database (DB2, MySQL, etc.), you'll run into this type of construct. The same is true, of course, if you're writing data out to a file or page.
Following this basic format, you can create three or even more dimensional arrays, but the question then is, what would they represent?
Of greater interest is what we do now that we have data in an array, be it one- or 20-dimensional. While PHP supports a number of variable formats, arrays are one of the most useful for people who are doing business applications. And when you're developing application programs, you'll use arrays to hold both page data (data that's going to be displayed) and database information. This might have come from an RPG data structure, passed in on the call, where we're moving data out to a page or file, or else destined for an RPG data structure to be massaged and used by the program.
Either way, one of the most important activities after learning how to load data in a number of different types of arrays (numeric, associative one-dimension, associative multi-dimension) is how to pull that data out and display or post it. And that will be the topic of our next column. See ya!
|Last Updated on Wednesday, 17 July 2013 00:00|