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! Quote Link to comment 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 Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.