 |
|
Oracle Database Tips by Donald Burleson
|
PHP Functions
As with the case of every proper programming
language, PHP can be used to create custom functions and provides a
wealth of built-in functions. This section both demonstrates how to
build custom functions and describes very useful ones that are used
later.
Function declaration is very simple as shown
below:
function
<function name> ($arg1,$arg2,$arg3)
{ statement block; }
The following is an example of a simple function:
#!/usr/local/bin/php
<?php
function make_bold($str) {
$str="<B>".$str."</B>";
return($str);
}
$a="This is a string.";
echo "Make bold:".make_bold($a)."\n".
"Original string:".$a."\n";
?>
When executed, this function displays the
following:
$
./example8.php
Make bold:<B>This is a string.</B>
Original string:This is a string.
The return statement in the function
above terminates the execution of the function and returns the value
to the program. Functions in PHP use the "call by value" method,
instead of "call by reference". What does this mean? The arguments
passed to a function are copies of the actual variables, and changing
the argument in the function does not change the variable itself.
Variable $a in the above example is used as
an argument to the make_bold function, but is not changed in
any way by the assignment in the function. As is seen from the result,
the value of the variable $a remains the same before and
after the execution of the function.
The following is the above example in modified
form:
#!/usr/local/bin/php
<?php
function make_bold(&$str) {
$str="<B>".$str."</B>";
return($str);
}
$a="This is a string.";
echo "Make bold:".make_bold($a)."\n".
"Original string:".$a."\n";
?>
The only difference is the ampersand character (&)
in front of the function argument; the $str variable. The
ampersand means that the argument is passed "by reference" or that the
argument passed to the function is the actual variable and not a
cloned copy. Modifying the function argument within the function
actually modifies the original variable passed for the argument
substitution. So, executing the script above gives a slightly
different result:
$
./example8a.php
Make bold:<B>This is a string.</B>
Original string:<B>This is a string.</B>
As seen in the result, the value of the variable
$a is changed to include the HTML "bold" tags. A function does
not need to include the return statement. The function requires
a return statement only if it needs to return a value to the main
program, though, this not strictly true. Values can be communicated to
and from the function using global variables, but it is a practice to
be used with great caution.
In fact, as any programming manual reveals, global
variables are "evil". More on this topic is explained in the section
regarding the variable scope. For now, this section focuses on the
global function properties.
The next thing to mention when speaking about
functions is recursion; a function can call itself. This is usually
illustrated using the factorial function as an example:
#!/usr/local/bin/php
<?php
function factorial($n) {
if ($n>0) return($n*factorial($n-1));
else return(1);
}
$a=5;
echo "Factorial($a)=".factorial($a)."\n";
?>
Execution of this example gives the predictable
result seen earlier in the section regarding the loops:
$
./example9.php
Factorial(5)=120
In the expression return($n*factorial($n-1)),
the function calls itself. This is called recursion. Recursion
cannot be infinite and must have a terminating condition. In the case
of the factorial function, the terminating condition is the "else
return(1)". This condition terminates the recursion when the
argument becomes less than or equal to zero. Recursion is convenient
for some processes, but because of the changing contents, it imposes
an additional burden upon the interpreter. In fact, a well written
loop is almost always faster then a recursion.
Another useful fact to mention is that a function
can contain HTML code as is visible from the following example. This
example is slightly more complex than has been presented so far:
<html>
<head>
<title>Example 10</title>
</head>
<body>
<?php
$isused=0;
function factorial($n) {
if ($n>0) return($n*factorial($n-1));
else return(1);
}
function get_number() {
global $isused;
?>
<form
action=<?=$_SERVER['PHP_SELF']?> method="post">
<center>
<h2>Computing Factorial</h2>
</center>
<hr>
Number:
<input type="text" name="number" size="10"
value= <?php
if (isset($_POST['number']))
echo $_POST['number'];
else echo "0";
?>
maxlength="32">
<input type="submit" name="submit">
</form><br>
<hr>
<?php
if (isset($_POST['number'])) {
$isused=strlen($_POST['number']);
return($_POST['number']);
}
else {
$isused=0;
return(1);
}
}
$n=get_number();
if ($isused): ?>
<center>
<b>Factorial:<? =factorial($n) ?>
</b>
</center><?php endif; ?>
</body>
</html>
When this example is executed, the output looks
like the following:
Here is a mixture of HTML and PHP, a form, strange
declarations ("global"), strange functions("strlen") and strange
variables ($_SERVER['PHP_SELF]) and $_POST['number']).
What does this all mean? Starting with the
declaration "global", one
finds that variables defined in functions are called local variables
and usually are not visible outside scope of the function. To make
them globally visible, variables are declared as "global". In other
words, global variables are just synonyms for the same variable from
the main program. In the example, the variable $isused is
global. If the submit function is pressed, the form is used (or
initialized) and its variables can then be used.
This event is monitored by the variable $isused.
Global variables should be used sparingly since they are difficult to
debug because of inadvertent conflicts with other variables. Global
variables are normally used exactly as in the above example as status
variables or indicator variables. Speaking of global variables, the
authors of PHP provide a myriad of predefined ones. The full list can
be located at the following address:
http://us2.php.net/variables.predefined
All of these variables are associative arrays
(hashes).
$_SERVER['PHP_SELF'] points to the
currently executing file. $_POST['number'] contains the value
of the field called 'number' from the HTML form which transmits values
by POST method. Each field in the form has associated a corresponding
variable in the $_POST array, or if the form is using the GET
method, in the $_GET array. Below is the full list of
predefined variables copied from the web site above.
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. |
|