PHP Functions
PHP Functions
BY
MAMATA PANDEY
Functions
A function is a block of statements that can be used repeatedly in a program.
A function will not execute automatically when a page loads.
A function will be executed by a call to the function.
Syntax:
function functionName() {
code to be executed;
}
Example:
<?php
function writeMsg() {
echo "Hello world!";
}
setHeight(350);
setHeight(); // will use the default value of 50
setHeight(135);
setHeight(80);
?>
Returning values
Return statement is used to return value from a function
Example
<?php declare(strict_types=1); // strict requirement
function sum(int $x, int $y) {
$z = $x + $y;
return $z;
}
Example:
<?php
function add_five(&$value) {
$value += 5;
}
$num = 2;
add_five($num);
echo $num;
?>
Thank You