PHP Variables Default Value


 

PHP Variables Default Value

In this tutorial we will learn how to set default value in PHP variables or Functions. If there is no value in the variable passed to the function call than only the default value is used by the function.

In this tutorial we will learn how to set default value in PHP variables or Functions. If there is no value in the variable passed to the function call than only the default value is used by the function.

PHP Variables Default Value


In this tutorial we will learn how to set default value in PHP variables or Functions. If there is no value in the variable passed to the function call than only the default value is used by the function. Here, we also learned how to pass information to the function through function call. For example :

<?php function Welcome($current_visitor)

{

    echo "Hello ".$current_visitor;

}

Welcome("Suraj");

?>


The output of the above example is : Hello Suraj.
In the above example we define function Welcome which has only one variable or you can say argument variable that is $current_visitor. In the function call we passed the variable value called Suraj. If there is no value passed in the function call the only thing will shown in the screen is Hello. Let see the example where we haven't passed any value in the variable argument.

 

<?php

function Welcome($visitor="People!")

{

    echo "Hello ". $visitor;

}

Welcome();

?>


You can notice that the default value is set by assigning the value to the argument variable within the parenthesis where the function defined . This value will be used by everyone, if no other value is passed through function call. You can see in the function call the parenthesis are blank, it means no other value have been passed in the function.

Ads