happy cat image

Start Web Coding

Your First step toward web coding,
Start Web Coding

change UI

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.

강좌로 돌아가기