Jump to content

ternary operator help please!


alexweber15

Recommended Posts

having 2 issues here can someone plz help me out?

 

1-

if(condition){ return $variable1; } else { return $variable 2;} 

 

how can i write this as a ternary statement?

 

return condition ? $variable1 : $variable2;

doesnt work and neither does this:

condition? return $variable1 : return $variable2;

 

 

and most importantly:

 

2-

    if($this->settings['stickyID']){
        $this->settings['id'] = &$this->settings['name'];
    }else{
        $this->settings['id'] = $this->settings['name'];
    }

 

i wanted to rewrite this as: (stickyID is bool btw)

 

$this->settings['id'] = $this->settings['stickyID'] ? &$this->settings['name'] : $this->settings['name'];

 

much simpler right?  i get an error with the & though....

 

thanks! :)

Link to comment
Share on other sites

return (condition) ? $var1 : $var2;

 

should work

 

As for the reference, the & goes with the =, not the variable when assigning.. so should probably use an if statement there.

 

edit: nevermind about the other example I gave, it won't work :)

 

funny thing is the first example works in javascript if im not mistaken...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.