Posts Tagged ‘default’

parameters with default values

Post by: bingorabbit
Published on June 11th, 2008
Filed under: information.technology,linux,my.hacks,php,web
Tags: , , , ,

It’s known that declaring a php function has the following syntax:

< ?php
function function_name(Parameters_list) {
// function
// code
// block
}
?>

an example of this can be like this:

< ?php
function nameAndAge($name,$age){
echo "Welcome $name, you are $age years old!";
}
?>

When I issue the following statement:

< ?php
nameAndAge("bingorabbit",20);
?>

I will get:
Welcome bingorabbit, you are 20 years old!

Most of us know that, and also we know that to set a default value for any parameter, we can do the following:

< ?php
function function_name($parameter1 = "default value", rest_of_the_parameter_list) {
// function
// code
// block
}
?>

which might take the following form:

< ?php
function nameAndAge($name="bingorabbit",$age){
echo "Welcome $name, you are $age years old!";
}
?>

so when I issue:

< ?php
nameAndAge();
?>

I will get:
Welcome bingorabbit, you are years old!

and when I issue:

< ?php
nameAndAge("Ahmed", 15);
?>

I will get:
Welcome Ahmed, you are 15 years old!

The problems is such declaration is that, you can’t just issue:
(more…)