The second line of output, the single use of MD5, does not change on a page refresh,
while the other content does.
There is more detailed discussion on the MD5 function (and its more
secure cousin sha1) in Chapter 9 on security.
PHP provides many more string functions, and over time you may choose to become
familiar with many of them. The string functions we have covered here are those that
you are likely to find the most beneficial right away. In the next chapter, we will follow
a similar pattern with a discussion of arrays.
String Functions (Best of) | 43
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
CHAPTER 5
Arrays
Now that we have a handle on the concept of strings, let’s take a look at the power and
flexibility of arrays. Arrays are known as compound data types; all that really means is
that they are more complex in structure than simple strings and integers, which are also
known as scalar data types. Imagine an array as an egg carton. It carries 12 individual
compartments that can house one egg each, yet it travels around as one entity. The
compartments can be broken off and made into single egg holders, or holders in any
number combination. Additionally, these compartments are not limited to holding only
eggs; they can hold rocks, or cookies, or match sticks. Of course, an analogy like this
has its limitations—egg cartons cannot easily be expanded and they cannot hold other
egg cartons, all of which, we will see, arrays are excellent at doing.
Let’s talk more precisely about arrays. Like egg cartons, arrays have compartments
(elements) that hold data. The elements and their respective data always travel together
(although you can have an empty element without data) and are known as key/value
pairs. So, if we had an array of five elements, each containing a number in the range
from 1 to 5, it would look like Table 5-1 (elements start their counting with 0).
Table 5-1. Visual representation of an array with numeric (indexed) keys
Keys 0 1 2 3 4
Values
viding the key number), the result is that the next available integer key will be assigned
to that element for us.
Creating an array in this fashion may or may not be what you want. The other method
available is to use the array function with the key/value pairs passed together and
separated by commas, like so:
$myArray = array(0 => 1, 1 => 2, 2 => 3, 3 => 4, 4 => 5) ;
This way of creating an array is much more condensed, yet it may be a little more
difficult for humans to read.
If you want to create an empty array, just use the array function without any parameters:
$a = array()
The keys of any array have to be named uniquely, otherwise there would
be
confusion when referencing their contents. PHP won’t prevent you
from using the same key multiple times in an assignment, but each
identical key assigned will replace the one before it.
Associative Arrays
So far we have looked at indexed arrays, but as I mentioned before, the key portion of
an array can also consist of character strings. The data values that we have looked at
so far have also been numerical, but these too can be changed to string data, as shown
in Table 5-2.
46 | Chapter 5: Arrays
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Table 5-2. Visual representation of an array with named (associative) keys
Keys first second fname initial lname
Values
1 2 Peter B MacIntyre
To create the array represented in Table 5-2, use the same code syntax options as before,
but with appropriate alterations for the strings:
$myArray['first'] = 1 ;
$myArray['second'] = 2 ;
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
It’s a good idea to familiarize yourself with two-dimensional arrays, as
they are the main structural representation of database results. More on
that in Chapter 7.
To make reference to the elements of a second (or deeper) dimension, just continue
with the square brackets concept. To refer to the element in our example that contains
the string “Tomato,” do this:
echo $myArray['fruit'][2] ;
Remember that the elements are counted beginning with zero, so you will need to ask
for element 2 to get the third one. This is true even though we assigned strings to both
the keys and the values. And yes, a tomato is a fruit!
Arrays Can Be Dynamic
As you have probably already realized, arrays in PHP are dynamic. This means that
with the use of the right code commands and functions, you can add elements to an
existing array without much effort. You can also delete elements from an array just as
easily. In fact, you can do a lot of things to arrays (more of which we will examine later
in this chapter), but for now let’s just look at adding to and taking away from them.
To add an element to the end of an existing array, simply use the empty square brackets
approach.
If you are adding to the end of an associative array and fail to provide a
key,
PHP will add the element with the next highest index value, thus
making a mixed index and associative array.
Let’s look at some code:
$myArray = array('first' => 1, 'second' => 2,
'fname' => "Peter", 'initial' => "B", 'lname' => "MacIntyre") ;
echo $myArray['fname'] . " " . $myArray['initial']
. " " . $myArray['lname'] ;
echo "<br/>" ;
$myArray[] = "555-5678" ;
["initial"]=> string(1) “B” ["lname"]=> string(9) “MacIntyre” }
The first option in the array_splice function is the array to be worked on, and the
second option is the array position with which to start the work. In this case, we are
telling PHP to remove the fifth element from this array. Notice that we are using the
index position value here, 4, and not the key value of 0. You can use a conditional third
option, length, which indicates how many elements this work should be performed on.
Since our code does not use this option, the default action is to perform the task on the
last element of the array. If you want to maintain the original array and make a new
one that will hold the result of the array_splice function, simply assign the result to a
new variable and array_splice will create a completely new array for you. We can
change the code to remove all the contents of the array that have to do with my name
and place them into a new array with the following code:
$myArray = array('first' => 1, 'second' => 2,
'fname' => "Peter", 'initial' => "B",
'lname' => "MacIntyre", 'phone' => "555-5678") ;
$name_array = array_splice($myArray, 2, 3);
var_dump($myArray) ;
echo "<br/>" ;
var_dump($name_array) ;
The output would be thus:
Arrays Can Be Dynamic | 49
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
array(3) { ["first"]=> int(1) ["second"]=> int(2) ["phone"]=> string(8) “555-5678” }
array(3) { ["fname"]=> string(5) “Peter” ["initial"]=> string(1) “B” ["lname"]=> string(9)
“MacIntyre” }
Notice
here, too, that the array_splice function leaves the phone number as the last
element in $myArray, effectively lifting out the elements that have to do with the name.
This is accomplished with the third option (the limit option) in the array_splice
function.
portion of the element and provide you with just the corresponding values. Alternately,
50 | Chapter 5: Arrays
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.