alexweber15 Posted October 6, 2008 Share Posted October 6, 2008 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 More sharing options...
genericnumber1 Posted October 6, 2008 Share Posted October 6, 2008 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 Link to comment https://forums.phpfreaks.com/topic/127277-ternary-operator-help-please/#findComment-658271 Share on other sites More sharing options...
alexweber15 Posted October 6, 2008 Author Share Posted October 6, 2008 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 https://forums.phpfreaks.com/topic/127277-ternary-operator-help-please/#findComment-658329 Share on other sites More sharing options...
DarkWater Posted October 6, 2008 Share Posted October 6, 2008 return (condition) ? TRUE_VAR : FALSE_VAR works for me, so why don't you show us what you actually tried, instead of: return condition ? $variable1 : $variable2; Link to comment https://forums.phpfreaks.com/topic/127277-ternary-operator-help-please/#findComment-658338 Share on other sites More sharing options...
genericnumber1 Posted October 6, 2008 Share Posted October 6, 2008 Yeah, when I said it should work, I should have been distinct and said it does work. Link to comment https://forums.phpfreaks.com/topic/127277-ternary-operator-help-please/#findComment-658341 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.