Databases Reference
In-Depth Information
The PHP manual web site ( http://www.php.net/manual ) has excellent search and
browse features for locating details on functions. When you visit this page, you'll see
a search box at the top right. By default, if you type text and press Enter (or click the
small right-arrow icon), you'll search the function library names for exact or near
matches. For example, if you type print and press Enter, you'll be taken directly to the
manual page for the print statement. If instead you type prin , you'll be taken to a page
of near matches, including links to print , printf , sprintf , and related entries. Very
close matches are shown in bold, while less likely matches are shown without bold
(and, in this example, include functions such as phpinfo , phpinfo , and pi that make
passing reference to printing in their descriptions).
You can also define your own functions. User-defined functions are created with the
keyword function and enclosed in braces. Here's an example of a user-defined function
do-math( ) that itself calls PHP math library functions to output interesting values:
function do-math($x)
{
if ($x > 0)
{
print "log10(x) = " . log($x,10);
print "logN(x) = " . log($x);
print "sqrt(x) = " . sqrt($x);
print "x^2 = " . pow($x, 2);
return true;
}
else
return false;
}
// Print out interesting math for the value 10
$ret = do-math(10);
// This test should fail, since the function should return true for 10
if ($ret == false)
print "Can't do math for <=0";
// Now, try to print out interesting math for the value 0
$ret = do-math(0);
// This test should succeed and print the error message,
// since the function should return false for 0
if ($ret == false)
print "Can't do math for <=0";
The function returns true when the parameter is greater than 0, and false otherwise.
In the example, the return value is assigned to $ret and used in the subsequent if test.
Passing variables by reference
If you add the ampersand symbol ( & ) before the name of a variable in a function dec-
laration, a reference to the variable will be passed to the function, rather than the var-
 
Search WWH ::




Custom Search