 |
|
Oracle Database Tips by Donald Burleson
|
Unlike Perl, the arrays in PHP have no special
notation. Array variables are marked prefixed with the dollar sign,
not with the '@" or '%' as in Perl. Arrays are collections of a finite
number of elements accessible either by their number or by a key. The
following are two basic types of arrays:
-
$a=array("This","is",
"an","array","of","strings");
-
$b=array("This"=>1,"is"=>2,"a"=>3,"hash"=>7);
The first way of defining the array makes it
accessible through a numerical index. The assignment $c=$a[1];
assigns the value of "is" to the variable $c. Assignment
$c=$a[0]; assigns the value "This" to the variable $c.
Array $b, on the other hand, is defined as an array accessed
through the textual key. Such arrays are called associative arrays or
hashes. Assignments such as $c=$b["hash"]; places the value 7
in the variable $c.
Arrays in PHP can be multidimensional and are
constructed from other arrays, just as matrices are constructed by
putting rows together in a bigger whole. Below is a little script
which constructs a 3x3 matrix $arr from arrays $a, $b and
$c.
<html>
<pre>
<?php
$a=array("a","b","c");
$b=array("d","e","f");
$c=array("g","h","i");
$arr=array($a,$b,$c);
print $arr[2][1];
?>
</pre>
</html>
When executed, this script displays the letter "h"
as a result. Array $arr is a matrix with 3 rows $a,$b and $c. Array
indexes start with 0 so $arr[2][1] points to the second element of the
third row, namely letter "h".
Arrays can also be empty. The assignment
$a=array(); assigns an empty array to variable $a. There is
a note worthy difference between NULL and an empty array; an empty
array can be populated, while NULL cannot. The array constructor
function "array" is not the only way to construct PHP arrays.
Arrays can be constructed simply by assigning values to their
elements, as shown in the next example:
$a[1]=10;
$a[2]=11;
$a[5]=17;
The array $a is so called a sparse array,
which means that the indexes are not contiguous numbers. If the script
below is executed by a web server, no errors are displayed and the
result in browser is the expected one: 11||17
<?php
$a[1]=10;
$a[2]=11;
$a[5]=17;
print $a[2]."|".$a[3]."|".$a[5];
?>
</pre>
</html>
If the same script is executed from the command
line, an error revealing the undefined array offset 3 is displayed.
$ php
book1.php
<html>
<pre>
PHP Notice: Undefined offset: 3 in
/home/mgogala/work/PHP/book1.php on line 7
11||17</pre>
</html>
$
Executing scripts from the command line is a good
development practice which saves much effort and catches syntax
errors. Errors marked as "Notice" or "Warning" will not terminate the
script execution. Yet, errors marked as "Error" will.
So, how should sparse arrays be handled while
trying to avoid the ugly errors such as the one above? The answer is
to use iterator functionssimilar
to those used in PL/SQL to deal with PL/SQL arrays. Iterators can be
thought of as smart indexes that remember position and know what the
next value is. The following list presents the iterator functions
along with a short definition of each:
-
Current($a)
- This function returns the value of the current value of the array
$a, with respect to the iterator.
-
Next($a)
- This function advances the iterator to the next position and sets
the value of the iterator (returned by current()) to the new
position.
-
Previous($a)
- This function moves to the previous position and adjusts the value
of the iterator.
-
End($a)
- This function moves the iterator to the last value and adjusts the
value of the iterator.
-
Reset($a)
- This function returns the value of the iterator to the beginning
of the array.
-
Key($a)
- This function returns the current value of key for the hashes.
-
Each($a)
- This function is used for loop control. It starts at the
beginning, advances to the next value and returns FALSE after the
last element effectively stopping the loop.
Detailed examples how to use iterators are
presented in the next section following the introduction of loops.
PHP has many functions that operate on arrays and
since this is not a PHP reference manual, no attempt to list every
function will be made. The best source to learn about a particular
function is through use of the PHP home page, located at
http://www.php.net.
However, the sort function
is worth special mention. This function sorts the elements of the
array. Many sort functions are used for different methods of
comparison and sorting order (numeric sorting vs. string, ascending
vs. descending, natural language sorting etc.) and all can be found on
the same page of the online manual.
Another crucial difference between Perl and PHP
must be mentioned. In Perl, it is perfectly legal to mix and match
arrays and scalars such as in the following example:
($a,$b,$c,$d)=@A;
Although this expression is legal in Perl, it is
illegal in PHP. The presence of the array context is indicated by
using the list function. If the variable $A is an array, the
Perlish assignment above is written as the following:
list($a,$b,$c,$d)=$A
Using array and list constructors is the paid
price of not having to mark array variables in a special way to
signify the array context.
At this point basic data types have been
explained. Now, what to do with these data types are presented.
See
code depot for complete scripts
This is an excerpt from the book
Easy Oracle PHP. You can get it
for more than 30% by buying it directly from the publisher and get
instant HTML-DB scripts from the code depot:
 |
Easy Oracle PHP
Create Dynamic Web Pages with Oracle Data
Includes online HTML-DB code depot
Buy it now for 30% off
- Only $19.95
|
HTML-DB support:
 |
For HTML-DB development support just call to gat an
Oracle Certified professional for all HTML-DB development
projects. |
|