Saturday, 24 April 2021

Php: Set default value to function argument

You can set default values to function parameters. If no value passed to this function, then this default value is considered.

 

Example

function welcome_user($name = "user"){

    echo "Hello $name, how are you\n";

}

 

welcome_user();

Since I am not passing any argument, it takes the string "user" as argument.

 

welcome_user("Krishna");

takes "Krishna" as function argument.

 

Find the below working application.

 

function_default_value.php

#!/usr/bin/php

<?php

function welcome_user($name = "user"){
    echo "Hello $name, how are you\n";
}

welcome_user();
welcome_user("Krishna");
?>

 

Output

$./function_default_value.php 

Hello user, how are you
Hello Krishna, how are you

 

 

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment