Prev | Current Page 204 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

You??™re free to output arrays using a looping conditional, such as
while or for; in fact, using these sorts of loops is required to implement many application features. I??™ll
return to this method repeatedly throughout this and later chapters.
134 CHAPTER 5 ?–  ARRAYS
Testing for an Array
When you incorporate arrays into your application, you??™ll sometimes need to know
whether a particular variable is an array. A built-in function, is_array(), is available
for accomplishing this task. Its prototype follows:
boolean is_array(mixed variable)
The is_array() function determines whether variable is an array, returning TRUE
if it is and FALSE otherwise. Note that even an array consisting of a single value will
still be considered an array. An example follows:
$states = array("Florida");
$state = "Ohio";
printf("\$states is an array: %s
", (is_array($states) ? "TRUE" : "FALSE"));
printf("\$state is an array: %s
", (is_array($state) ? "TRUE" : "FALSE"));
Executing this example produces the following:
$states is an array: TRUE
$state is an array: FALSE
Adding and Removing Array Elements
PHP provides a number of functions for both growing and shrinking an array. Some of
these functions are provided as a convenience to programmers who wish to mimic
various queue implementations (FIFO, LIFO, etc.), as reflected by their names (push,
pop, shift, and unshift).


Pages:
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216