Functions are a block of instructions that can be called multiple times in different parts of the program. Functions allow you to divide a program into smaller functional parts.
A function name must begin with an alphabetic character or underscore, followed by any number of alphanumeric characters or underscores.
function showText() { $text = "Hi"; }
To call a function:
showText();
function arguments:
function showText($a, $b) { echo $a + $b; }
We can now pass these arguments when the function is called:
myFunc(10,5);
We can also set a default value
function showText($a=10, $b=5) { echo $a + $b; }
Return function
Returns data from the function and stops it:
function showText($a, $b) { return 100; }
function returns 100;
pass the function as a string
function myFunc($a = 1, $b = 2) { return $a + $b; } $func = 'myFunc'; echo $funct();
[readaslo id=”854″]