parameters with default values
Filed under: information.technology,linux,my.hacks,php,web
Tags: default, functions, parameters, php, values
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:
-
<?php nameAndAge(15); ?>
-
<?php nameAndAge("",15); ?> -
<?php nameAndAge(,15); ?>
Thinking that $name will be replaced by its default value (bingorabbit) and $age will be replaced by 15, it will simply print:
- Welcome 15, you are years old!
- Welcome , you are 15 years old!
- it will throw an error immediately! or show a blank page.
The whole post is about a trick, that you can use to set the default values for a function parameter with being able to send only whatever parameter you wanna send without caring about other parameters. See Below:
For the first problem, you can simply do the following:
<?php
function nameAndAge($age, $name="bingorabbit"){
echo "Welcome $name, you are $age years old!";
}
?>
then you can issue:
<?php nameAndAge(30); ?>
and it will print:
Welcome bingorabbit, you are 30 years old!
In PHP 4, parameters with default values should come in the first left side of the function parameter list declaration, now in PHP 5 you can put it at the end, middle or any where in the function’s parameter list declaration.
For the second problem you can do the following:
<?php
function nameAndAge(){
if(func_num_args() > 0){
$name = func_get_arg(0);
$age = func_get_arg(1);
if (!$name) $name = "bingorabbit";
if (!$age) $age = 15;
echo "Welcome $name, you are $age years old!";
}
}
?>
Using this way, you can simply issue:
<?php
nameAndAge("",31);
?>
or
<?php
nameAndAge("ahmed",'');
?>
This is a solution for if you don’t want to use send all parameters, but in such case you will have to send empty values for parameters you don’t wanna send.
This solves one of the problems, the second one.
For sure all that is very restricted to the number of parameters sent, but now it can be customized using the introduced functions in the post:
func_num_args() which returns the number of arguments sent to a function.
func_get_arg(arg_num) which returns the argument based on its number starting from 0 to the very first parameter.
Hope the post is useful and you got my idea.
This one is especially for Osama Gamal Atia, he asked me about it in the course and I told him the answer which came to my mind then and today I have found a new answer so I had to come back here and share it with you.








URGHHGHHH!!
I have two thoughts on mind right now. One, this totally rocks! PHP has become extremely flexible that I don’t even need to bother considering how many parameters I need to deal with.. and the second thought, this TOTALLY SUCKS! PHP has become extremely flexible just reading about it gives you the sense of flawlessness!
We all know what people do with JavaScript and how easily it could be hacked into. Who wants a more dangerous programming language that could potentially damage the users.
GAH!
Thanks though for sharing =) It’s the first new thing I’ve learnt in quite some time..
Apparently there are a number of functions that deal with functions.
A list could be found here.
Okay, now the technical post.
If you were going to use nameAndAge() with empty arguments you didn’t need to use those new functions. I don’t really think this implementation solves the previously encountered problem(s), you still have to give the arguments in the order they are expected.
This would have worked just as good:
< ?php function nameAndAge($name, $age){ if(!$name) $name = 'bingorabbit'; if(!$age) $age = 15; echo 'Welcome ' . $name . ', you are ' . $age . ' years old.'; } nameAndAge('',20); // prints Welcome bingorabbit, you are 20 years old. nameAndAge('ahmed',''); // prints Welcome ahmed, you are 15 years old. ?>A much better usage of those functions would be as follows:
< ?php function nameAndAge(){ $name = 'bingorabbit'; $age = 15; $argsLength = func_num_args(); $args = func_get_args(); if( $argsLength == 1 ) { if( $args[0] ) { if( (int)$args[0] > 0 ) { $age = (int)$args[0]; } else { $name = $args[0]; } } } else if( $argsLength == 2 ) { if( $args[0] ) { if( (int)$args[0] > 0 ) { $age = (int)$args[0]; } else { $name = $args[0]; } } if( $args[1] ) { if( (int)$args[1] > 0 ) { $age = (int)$args[1]; } else { $name = $args[1]; } } } echo 'Welcome ' . $name . ', you are ' . $age . ' years old.'; } nameAndAge(20); // prints Welcome bingorabbit, you are 20 years old. nameAndAge('ahmed'); // prints Welcome ahmed, you are 15 years old. ?>@kamasheto: I thought of the same script with exactly the same implementation, but I just mentioned the simple implementation not the “long” one
, anyway glad u like it and thanks for the last comment, really this completes the post
Thanks Bingo for your interest.
@Kama:
EL Code beta3k howa Solution le el Problem beta3t el “age, name” parameters bas
Consider mathlan paramters zay “address, name” or “mobile, phone”
So el code beta3k howa special case
Regards,
@Osama:
It’s always a good idea to have the original problem in mind when working on a solution, it helps not getting carried away with your imagination.
Regards.
aywa we el Original problem heya en ana kont bas2al BinGo law ana 3ayz ad5al value le el Second parameter aw third men 3′eer mada5al le el first we el second..
Then I suggest you read bingo’s post again.
la2 ya kama, dah 7aga kanet fel course, dah bas misunderstanding between both of you, I know what both of you mean
Gah.
I understand there are two problems, but when thinking in a programmable way to solve a problem I only think of solving what I can see.. mesh hanagem masalan trying to figure out any other problem that might have existed.
Nevertheless, this implementation could easily be hacked to make sure more than 2 arguments get through to your nameAndAge method. If one can’t see that straight away, one shouldn’t be attempting the first place.