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
https://forums.phpfreaks.com/topic/127277-ternary-operator-help-please/
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...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.