|
 |
|
Oracle Tips by Burleson
|
These are variables currently registered to a
script’s session. They are analogous to the old $HTTP_SESSION_VARS
array, still available, but deprecated. See the session handling
functions section for more information.
The Sessions, GET and POST method is covered in
detail in the next chapter. For now, focus remains on PHP and
functions.
In the example above, the global variable is a
variable from the main program imported into the scope of the
function. Global variables should not be confused with staticvariables, which retain their value between invocations.
Before static
variables are explained, another small detail from the example above
should be clarified: the functions “isset” and “strlen”.
The function “strlen” is the same as in C or C++ and returns
the length of the string given as the argument. The “isset”
checks whether the variable given as its argument is defined (“set”).
A variable is defined when it is referenced for the first time. These
two functions are two among the many useful functions provided by the
authors of PHP.
Focus can now return to staticvariables. What are they? They are variables that retain
the value between function invocations. Every local variable is
normally created at the beginning of the function invocation and
destroyed when the function returns. This means that a “normal”
variable is created anew for each invocation. Static variables are not
recreated each time a function is called.
How are they declared? It is simple. Instead of
using the word “global”, the word “static
” is used. What are they used for?
Static variables are mainly used as a counter, because they count the
number of times a function is invoked. The example for the static
variables is extremely simple and is a re-engineered example9.php,
with the preferred factorial function.
$ cat
./example9a.php
#!/usr/local/bin/php
<?php
function factorial($n) {
static $count=0;
$count++;
if ($n>1) return($n*factorial($n-1));
else { echo "Number of factorial invocations:$count\n";
return(1);
}
}
$a=6;
$b=factorial($a);
echo "Factorial($a)=$b\n";
?>
Variable $count
is declared as staticand
initialized to zero. Without the keyword “static”, the variable is
created during each invocation, set to zero and incremented by one.
The finalnumber of invocations
printed is “1” with the variable $count declared as static.
The output looks like it should:
$
./example9a.php
Number of factorial invocations:6
Factorial(6)=720
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. |
|