conker87 Posted April 11, 2008 Share Posted April 11, 2008 I've been looking into this alternative way of doing if statements. I'm still a little confused as to how and where I can use it. For instance, can I use it like this: echo ($foo) ? "bar" : "not bar"; Or will I have to do it like this: $test = ($foo) ? "bar" : "not bar"; echo $test; Link to comment https://forums.phpfreaks.com/topic/100649-blah-foo-bar-help/ Share on other sites More sharing options...
moon 111 Posted April 11, 2008 Share Posted April 11, 2008 You can use the first one. You can think of it like this: PHP first goes over the ?: bit and then executes the function or whatever. So echo ($foo) ? "bar" : "not bar"; is really either echo "bar"; or echo "not bar"; You can put it anywhere where "bar"/"not bar" is expected. Geddit? Link to comment https://forums.phpfreaks.com/topic/100649-blah-foo-bar-help/#findComment-514754 Share on other sites More sharing options...
haku Posted April 11, 2008 Share Posted April 11, 2008 They will both work, but the first one is better for echo statements. The second pattern (minus the echo) is what you use to set variables. Link to comment https://forums.phpfreaks.com/topic/100649-blah-foo-bar-help/#findComment-514771 Share on other sites More sharing options...
conker87 Posted April 11, 2008 Author Share Posted April 11, 2008 Aah, brilliant. Thanks a bunch (Y) Link to comment https://forums.phpfreaks.com/topic/100649-blah-foo-bar-help/#findComment-514777 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.