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:
The first way of defining the array makes it
accessible through a numerical index. The assignment $c=$a;
assigns the value of "is" to the variable $c. Assignment
$c=$a; 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
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 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:
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
If the same script is executed from the command
line, an error revealing the undefined array offset 3 is displayed.
PHP Notice: Undefined offset: 3 in
/home/mgogala/work/PHP/book1.php on line 7
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:
- This function returns the value of the current value of the array
$a, with respect to the iterator.
- This function advances the iterator to the next position and sets
the value of the iterator (returned by current()) to the new
- This function moves to the previous position and adjusts the value
of the iterator.
- This function moves the iterator to the last value and adjusts the
value of the iterator.
- This function returns the value of the iterator to the beginning
of the array.
- This function returns the current value of key for the hashes.
- 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
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:
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:
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.
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
||For HTML-DB development support just call to gat an
Oracle Certified professional for all HTML-DB development