jason360 Posted December 5, 2013 Share Posted December 5, 2013 (edited) Is it possible to put an if statement within a return? Not sure if this is possible. Thank you. Example: return ' <div id="comment"> </div> '; The if statement to go into the return: if ($a > $b) { echo "a is bigger than b"; } Edited December 5, 2013 by jason360 Quote Link to comment https://forums.phpfreaks.com/topic/284569-can-you-put-an-if-statement-within-a-return/ Share on other sites More sharing options...
Solution kicken Posted December 5, 2013 Solution Share Posted December 5, 2013 You can either use the ternary operator to do an in-line condition, or just split the return across two branches of a normal if/else. if ($a > $b){ return 'a is bigger than b'; } else { return 'a is less than or equal to b'; } or return ($a > $b)?'a is bigger than b':'a is less than or equal to b'; Quote Link to comment https://forums.phpfreaks.com/topic/284569-can-you-put-an-if-statement-within-a-return/#findComment-1461432 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.