You can use PHP's built in functions to make more complex user defined functions of your own. A function is created with a function statement like so:
function function_name( $argument_1, $argument_)
{
# code block
}
Your function may not require arguments but if it doesn't have any, you still have to use parentheses. Here is an example of a function that has no arguments:
<?
function blue_line()
{
echo "<center>
<div style="background-color: #000088; width: 80%; height: 8px">
</div>
</center>";
}
?>
I have that function above the HTML of this page and when ever I want a blue line, I call it just like a regular built in function.
<center>
<?
blue_line();
?>
</center>
The blue_line() function makes a pretty blue line but you could do the same thing just as easily with define() so let's move on to something fancier. PHP does color with RGB values but many other applications use hex values so it would be nice to have a function to convert one to the other. Conveniently, PHP has a built in function that converts decimal to hex: dechex(), but that isn't enough for this case. Consider what happens when you take the RGB values 1,1,1. If you convert those to hex and concationate them, you get 111 instead of 010101. You need a way to check the length of the hex outputs to decide whether to concationate a zero to it or not and for that, you use strlen().
<?
function rgb2hex($r, $g, $b)
{
$r=dechex($r);
$g=dechex($g);
$b=dechex($b);
if ( strlen("$r") == 1 )
{ $r = "0" . "$r"; }
if ( strlen("$g") == 1 )
{ $g = "0" . "$g"; }
if ( strlen("$b") == 1 )
{ $b = "0" . "$b"; }
$rgb = "$r$g$b";
echo "$rgb";
}
?>
That function can be stored on a file all by itself and added to any other file where you need it with include().
<?
$r=$_GET[r];
$g=$_GET[g];
$b=$_GET[b];
include("rgb2hex_f.php");
?>
<html>
<body bgcolor="#fad888">
<form>
<center>
<input type="text" name="r" size="3"
value="<? echo "$r"; ?>" /> Red /
<input type="text" name="g" size="3"
value="<? echo "$g"; ?>" /> Green /
<input type="text" name="b" size="3"
value="<? echo "$b"; ?>" /> Blue
<input type="submit" />
<input type="text" size="6" value="<?
if ( isset ($r) && isset ($g) && isset ($b) )
{
rgb2hex($r, $g, $b);
}?>">
</center>
</form>
</body>
</html>
Of course, you don't need a form to enter values into the function. You can enter numbers and variables into a user defined function just like a built in function. Since I used include() to include rgb2hex() into this page, I can use...
<?
$x = 200;
rgb2hex(10, 100, $x);
?>
For the examples above, the results are outputed to the browser. If you want to return the results to use in your script but not to the browser, you do that with return.
<?
function add( $x, $y)
{
$sum = $x + $y;
return ;
}
$z = 3 + add( 1,2 );
echo $z;
# returns 6
echo "This will not be executed";
?>
When the function executes a return statement, the function ends so the "This will not be executed" line is not executed. The sum of 1 and 2 is returned to the main script instead of the browser. The main script then adds 3 to it and echos 6.
|
|
|