Google News
logo
PHP Program to Function with default argument value
In the following example of PHP program that demonstrates a function with a default argument value :
Program :
<?php

// Declare a function with default argument value
function greet($name = "world") {
    echo "Hello, $name!";
}

// Call the function with default argument value
greet();

// Call the function with a different argument value
greet("John");

?>
Output :
Hello, world!
Hello, John!
In this example, we have declared a function `greet` that takes one parameter `$name` with a default value of `"world"`. Inside the function, we use the `echo` statement to print out a greeting message with the value of `$name`. If no argument is passed to the function, the default value of `"world"` will be used.

We then call the function `greet` twice - first without passing any argument (to use the default value), and then with the argument value `"John"`.

The output shows the greeting message with both the default and custom argument values.