Function
- Definition
- Case insensitive
- Scope
- Override
- Recursion
- Default argument values
- Variable-length argument lists
- Scalar type declarations & typed param
- Callback
Definition
function soma($a, $b) {
return $a+$b;
}
var_dump(soma(1)); //=> Fatal error: Uncaught ArgumentCountError: Too few arguments to function soma(), 1 passed
var_dump(soma(1, 2)); //=> int(3)
var_dump(soma(1, 2, 3)); //=> int(3)
Case insensitive
function soma($a, $b) {
return $a+$b;
}
var_dump(soma(1, 2)); //=> int(3)
var_dump(Soma(1, 2)); //=> int(3)
var_dump(SOMA(1, 2)); //=> int(3)
Scope
$name = 'fulano';
function hello() {
return "Hello $name"; //=> Notice: Undefined variable: name
}
var_dump(hello()); //=> string(6) "Hello "
Global
$name = 'fulano';
function hello() {
global $name;
return "Hello $name";
}
var_dump(hello()); //=> string(12) "Hello fulano"
Override
function soma($a, $b) {
return $a + $b;
}
function soma($a, $b, $c) {
return $a + $b;
}
//=> PHP Fatal error: Cannot redeclare soma()
Recursion
function factorial($n) {
return $n == 0 ? 1 : $n * factorial($n - 1);
}
var_dump(factorial(4)); //=> int(24)
Default argument values
function soma($a, $b, $c=0) {
return $a + $b + $c;
}
var_dump(soma(1, 2)); //=> int(3)
var_dump(soma(1, 2, 3)); //=> int(6)
Variable-length argument lists
function somation(...$values) {
return array_sum($values);
}
var_dump(somation(1, 2)); //=> int(3)
var_dump(somation(1, 2, 3, 4)); //=> int(10)
Scalar type declarations & typed param
function somation(int ...$values):int {
return array_sum($values);
}
var_dump(somation(1, 2)); //=> int(3)
var_dump(somation(1, 2, 3, 4)); //=> int(10)
var_dump(somation(1, 'a'));
//=> PHP Warning: Uncaught TypeError: Argument 2 passed to somation() must be of the type integer, string given, called in php
Callback
$helloWorld = function($name){
return "Hello $name";
};
var_dump($helloWorld('fulano')); //=> string(12) "Hello fulano"