happy cat image

everdevel

coding

login
NotificationX
  • Currently, only notices of comments are displayed.
  • no comment or please login

Setting parameter defaults for functions

We learned how to create a function earlier.
You can declare parameters when you create a function.
You can set the default value of a parameter when no argument is specified in the function call part.
For example, if you have the following code:

function plus($num, $num2, $num3) {
    $sum = $num + $num2 + $num3;
    return $sum;
}
echo plus();

In the code above, we didn't use any arguments.
This will cause an error.

How to set parameter defaults

function 함수명($param = default, $param2 = default) {
}

Now is it simple?
If there are no arguments, the default value will be used.
So let's create an example by setting defaults in the code we used above.

<?php
    function plus($num = 100, $num2 = 40, $num3 = 90) {
        $sum = $num + $num2 + $num3;
        return $sum;
    }
    echo plus();
?>

This is the result of the code above.


Thank you for visiting. If you have any inquiry or explanation of mistakes, please use the comments below.


    
    

Back to the course

ALL COMMENTS 0

Sort by