SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict agentforte's Avatar
    Join Date
    May 2007
    Location
    Toronto, ON, Canada
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Error with multiple default arguments in a function

    I have a function with default values that I need help with. Do you see anything wrong?
    Here is the code:
    PHP Code:
    function test($a$b$c=2$d='hello'$e='everyone') {
    echo 
    "$d $e <br /> a = $a<br /> b=$b<br /> c=$c<br />";
    }

    $val test('the letter a''the letter b''''''Mr. Bond');

    echo 
    $val
    the output I am trying to get is this:

    Hello Mr. Bond
    a = the letter a
    b = the letter b
    c = 2


    instead I get this:

    Parse error: syntax error, unexpected '=', expecting '&' or T_VARIABLE

    Please let me know:
    1) What is wrong with my code that gives me this error?
    2) What should I put for the $c and $d arguments to get a default value instead of blanks?

    Thank you!

    -Frank

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php

    function test ($a$b$c null$d null$e null) {

        
    $c = !is_null($c) ? $c 2;
        
    $d = !is_null($d) ? $d 'hello';
        
    $e = !is_null($e) ? $e 'everyone';
        
        echo 
    "$d $e <br /> a = $a<br /> b=$b<br /> c=$c<br />";

    }

    test('the letter a''the letter b'nullnull'Mr. Bond');
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Addict agentforte's Avatar
    Join Date
    May 2007
    Location
    Toronto, ON, Canada
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the suggestion but this did not work, I get the same error message.
    I am using PHP 5

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    It must be something else that snippet has no error
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    agentforte, the main problem with your initial function is that it echos directly, while your calling code expects a return value.

    Change the echo to return and it'll work, but the output won't be what you were aiming for.
    An empty string doesn't automatically cause an argument to be skipped...it just assigns $c and $d with empty strings. If you want to bypass them you'll need to add logic to do that inside the function (as demonstrated by logic_earth)

    Another option is also to put $e as the third argument, that way you can call your function with only 3 arguments and the defaults will apply to any arguments not passed.

    Make sense?

  6. #6
    SitePoint Addict agentforte's Avatar
    Join Date
    May 2007
    Location
    Toronto, ON, Canada
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    haha, sorry for being a newb. I missed a $ in front of one of the variables (The function is a lot more complex than the example I provided. I need some sleep and fresh eyes anyway...)

    The logic to set default values was really helpful.

    The third option Mike mentioned does not really apply, since some times $e will be provided and some times $d. So even switching would result in the same scenario (where a default is required before the last provided argument)

  7. #7
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, in that case passing null and implementing logic inside the function to replace null values with the default is the way to go.


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
  •