Function Params for Multiple Type Hinting
// Multiple type hinting in one parameter function myage(int|float|string $age){ echo $age; } myage("forty eight");
Here the $age parameter can be integer, float or string.
by dev
Function Params for Multiple Type Hinting
// Multiple type hinting in one parameter function myage(int|float|string $age){ echo $age; } myage("forty eight");
Here the $age parameter can be integer, float or string.
by dev
Type Hinting
// Type Hinting - int, float, bool, string
function myage(int $age){ echo $age."\n"; } myage(5);
by dev
Sometimes functions with parameters need to be set with a default value to make it optional.
// Function with Params Default value
function sum($x,$y=5){
$num1=$x;
$num2=$y;
echo $num1+$num2.”\n”;
}
sum(2);
sum(5,9);
Here $y=5 is the default value of parameter $y.
by dev
Adding parameter(s) within function called functions with Params.
//functions with params function sum($x,$y) { $num1=$x; $num2=$y; echo $num1+$num2."<br />"; } sum(2,3); sum(4,5); sum(100,300);
by dev
User-defined functions are functions created by the programmer. They’re defined using the function keyword. The functions are different from built in function, and reusable.
// user defined function function sum() { $num1=10; $num2=20; echo $num1+$num2; } sum(); // call the function