The parameter may be empty. That is null.
// Nullable type hints – ?
function sum(?string $age){
echo $age;
}
sum(“4.5”);
sum(null);
by dev
The parameter may be empty. That is null.
// Nullable type hints – ?
function sum(?string $age){
echo $age;
}
sum(“4.5”);
sum(null);
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);