Hudzilla.org - the homepage of Paul Hudson
Contents > Functions > User functions Wish List | Report Bug | About Me ]

4.15.5     Default parameters

This is NOT the latest copy of this book; click here for the latest version.

When designing your functions, it is often helpful to be able to assign default values for parameters that aren't passed - PHP does this for most of its functions, and it saves you having to pass in parameters most of the time if they are usually the same.

To define your own default parameters for a function, simply add the constant value you would like them set to after the variables, like this:

<?php
    
function doFoo($Name = "Paul") {
        return
"Foo $Name!\n";
    }

    
doFoo();
    
doFoo("Paul");
    
doFoo("Andrew");
?>

That script will output the following:

Foo Paul!
Foo Paul!
Foo Andrew!

Now, consider this function:

function doBar($FirstName, $LastName = "Smith") { }

Does that mean that both $FirstName and $LastName are set to Smith? As it happens, it is only $LastName - PHP treats the two variables as functionally independent of each other, which means you can use code like this:

function doBar($FirstName = "John", $LastName = "Smith") {
    return
"Hello, $FirstName $LastName!\n";
}

So, to greet someone called John Smith, you could just use this:

doBar();

To greet someone called Tom Davies, you would use this:

doBar("Tom", "Davies");

To greet someone called Tom Smith, you would use this:

doBar("Tom");

Now, how would you greet someone called John Wilson? Ideally you would let PHP fill in the first parameter for you, as John is the default for the function, and you would provide the Wilson part. But if you try code like this, you will see it does not work:

doBar("Wilson");

Instead of John Wilson you will get Wilson Smith - PHP will assume the parameter you provided was for the first name, as it fills its parameters from left to right. The same logic dictates that you cannot put a default value before a non-default value, like this:

function doWombat($FirstName = "Joe", $LastName) { }

If someone used doWombat("Peter"), would they be trying to provide a value for $FirstName to use instead of the default, or do they want the default value in there and Peter was for $LastName? Hopefully you can see why PHP will flag up an error if you attempt this!





<< 4.15.4 Returning by reference   4.15.6 Variable parameter counts: func_num_args(), func_get_arg(), and func_get_args() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
A PHP User - 05 Dec 2008

darn it, second again!!

A PHP User - 05 Dec 2008

for the first example, i donno if it's php 4 or 5, but I'm using php 4, and it will not return anything without using echo or print on the functions to display.



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is seven plus seven?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow