PHP has a lot of built-in functions, also you can create and use your own functions.
Functions helps you to organize the various parts of a script into different tasks that must be accomplished.
To keep some PHP instructions from being executed when the page loads, you can put it into a function.
function function_name() { // Function code. }The name of your function must begin with either a letter or the underscore, fallowed by any combination of letters, numbers, and the underscore. You cannot use an existing function name (print, echo, isset, and so on).
<?php function sayHy() { echo 'Hy there, how are you?'; } ?>
function function_name($param1, $param2, ...) { // Function code. }The parameters $param1, $param2, etc. are variables passed into the function. These variables only matter within their function, and they don't interfere with other similar variables outside the function.
<?php function sayHyTo($name) { echo 'Hy '. $name. ', how are you?'; } ?>
function_name();A function with parameters can be called using the syntax:
function_name($arg1, $arg2, ...);- "$arg1", "$arg2", ... are the arguments passed to the function. They are just like variables, and pass values to the function's parameters.
<?php // function without parameters function sayHy() { echo 'Hy there, how are you?'; } // function with parameters function sayHyTo($name) { echo 'Hy '. $name. ', how are you?'; } // caling the function without parameters sayHy(); // Hy there, how are you? echo '<br />'; // using the function with parameters sayHyTo('Victor'); // Hy Victor, how are you? ?>- As you can notice, the parameters add more functionality to a function. You can use the second function ( sayHyTo() ) to say "Hy ..." to different names.
function function_name($param1, $param2, ...) { // Function code return value; }A function can return a value (a string or a number) or a variable whose value has been created by the function.
<?php function getMed($x, $y) { $val = ($x + $y) / 2; return $val; } $med = getMed(8, 10); echo $med; // 9 ?>
The return statement terminates the code execution at that point, any code within a function after the return instruction will never run.
To have a function return multiple values, use the array() function to return an array:
return array(elm1, elm2, elm3);
<?php function add($x, $y=5) { $total = $x + $y; return $x. ' + '. $y. ' = '. $total; } // calls the add() function with one argument (for the $x parameter) echo add(14); // 14 + 5 = 19 // calls the add() function with both arguments echo '<br />'. add(14, 8); // 14 + 8 = 22 ?>When the value for the second parameter ($y) is not pased, the function uses its default value (5).
<?php function factorial($nr) { if ($nr>0) $re = $nr * factorial($nr-1); elseif ($nr==0) $re = 1; else $re = null; return $re; } echo '9 factorial is: '. factorial(9); // 9 factorial is: 362880 ?>This code will output:
<input type="checkbox" name="a_name" value="value" checked="checked" />
#id { background:url("path_to_image.png"); background-size:contain; background-repeat:no-repeat; }
var rest8_7 = 8 % 7; alert(rest8_7);
$nr = ceil(3.5); echo $nr; // 4