SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: compare these functions, is there a benefit to using null as a default argument

  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    compare these functions, is there a benefit to using null as a default argument

    One function has $toPage = null as the argument, the other function just uses $toPage. Why use null?

    Code PHP:
    function redirect_page($toPage = null){
            if ($toPage != null){
                header("Location:".$toPage);
                die();
            }
        }
     
    function redirect_page($toPage){
            header("Location:".$toPage);
            die();
        }

  2. #2
    Barefoot on the Moon! silver trophy
    Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    3,659
    Mentioned
    21 Post(s)
    Tagged
    1 Thread(s)
    It depends on how the function is used and what is being passed in.

    Considering that there's only one argument being passed, I might not opt to include a default value if there is a check/filter for input elsewhere.

    If there were multiple arguments which might not always have a value, then it might be more appropriate to assign a default value like that.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2010
    Posts
    83
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Understood. Thanks.

    Is it true that with multiple arguments, if one or more arguments have a default value of null that it must be last in the argument list?

    i.e.

    Code PHP:
    function example($value1, $value2 = null){
     
        }

    if not is there any situation where this is necessary?

  4. #4
    Barefoot on the Moon! silver trophy
    Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    3,659
    Mentioned
    21 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by sessions View Post
    Understood. Thanks.

    Is it true that with multiple arguments, if one or more arguments have a default value of null that it must be last in the argument list?

    i.e.

    Code PHP:
    function example($value1, $value2 = null){
     
        }

    if not is there any situation where this is necessary?
    Typically, optional arguments *are* placed last so that when the function is called, you could just call it with the required argument and leave out the optional argument (the one with an existing default value): example("value_a")

    There's no rule preventing you from actually changing the order, but if you did, then you would have to call the function with both arguments or the function call wouldn't work properly. Example:

    Code:
    function example(value2=null, value1){}
    Code:
    example("value_b","value_a");
    Also, see example #5 here: PHP: Function arguments - Manual
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •