PHP allows you to define C++ style default argument values. In such a case, if you don't pass any value to the function, it will use default argument value.
A simple example of using PHP default arguments in the function.
<?php
function myDefault($name="Suhail"){
echo "Hello $name<br/>";
}
sayHello("Karan");
sayHello();//passing no value
sayHello("Sonia");
?>
Output:
Hello Karan
Hello Suhail
Hello Sonia
Note: Since PHP 5, you can use the concept of default argument value with a call by reference also.
<?php
function myDefault($first="Sonia",$last="Sharma"){
echo "My Name: $first $last<br/>";
}
myDefault();
myDefault("Karan");
myDefault("Suhail","Khan");
?>
Output:
My Name: Sonia Sharma
My Name: Karan Sharma
My Name: Suhail Khan
<?php
function addCal($first=10,$second=20){
$sum=$first+$second;
echo "Addition is: $sum<br/>";
}
addCal();
addCal(90);
addCal(50,40);
?>
Output:
Addition is: 30
Addition is: 100
Addition is: 90
© 2024 Easy To Learning. All Rights Reserved | Design by Easy To Learning