Yangshuo Posted November 14, 2009 Share Posted November 14, 2009 Fairly new to PHP.. Is there a way to set the value of a variable using conditional logic similar to the following in javascript?: var isPostive = (x > 0)?true:false Thanks! Link to comment https://forums.phpfreaks.com/topic/181500-solved-set-variable-with-condition-statement/ Share on other sites More sharing options...
Yangshuo Posted November 14, 2009 Author Share Posted November 14, 2009 haha, I just tried the same (a)b?c:d format in php and it worked. Guess I should have tried it before posting Anyway, question solved Link to comment https://forums.phpfreaks.com/topic/181500-solved-set-variable-with-condition-statement/#findComment-957412 Share on other sites More sharing options...
rarebit Posted November 14, 2009 Share Posted November 14, 2009 Not got a refernce with me but: echo "Result: ".(($isPostive>0)?true:false)."<br />"; or something like that. Or are you actually referring to Javascript and not PHP? Link to comment https://forums.phpfreaks.com/topic/181500-solved-set-variable-with-condition-statement/#findComment-957413 Share on other sites More sharing options...
j0n Posted November 14, 2009 Share Posted November 14, 2009 for your reference (if you need to refer to this again), it is called a ternary operator. var isPostive = (x > 0)?true:false is also a waste, since an IF statement is a boolean operation anyway. It can be rewritten in PHP as: $isPositive = ($x > 0); Hope this helps. Link to comment https://forums.phpfreaks.com/topic/181500-solved-set-variable-with-condition-statement/#findComment-957416 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.