There are two categories of functions; those built in to PHP and those you define yourself. There are hundreds of functions built into PHP. A function takes some kind of input, called an argument and usually returns some kind of output. A function may accept more than one argument. If it does they are separated by commas.
function_1( $argument_1, $argument_2, $argument_3 );
An example of a function that accepts a single argument is the square root function
<?
$x = 25;
$y= sqrt($x);
echo "$y"; # returns 5
?>
An example of a function that accepts a two arguments is the pow() function:
<?
$x = 2;
$y = 3;
$z = pow($x, $y);
echo $z; # outputs 8 (2 to the 3rd power)
?>
Any kind of variable may be an argument including numbers, strings, arrays and objects. The results of one function may be the argument of another:
function_2(function_1($var));
The code below will first find the square root of 25 then raise the result to the 3rd power:
<?
$x = 25;
$x = pow( sqrt($x), 3 );
echo $x; # outputs 125
?>
There are many more functions, more than the scope of this tutorial but I made some short lists of functions with examples. For a complete list, see the PHP Manual.
|
|
|