jason360 Posted December 5, 2013 Share Posted December 5, 2013 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"; } 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...
kicken Posted December 5, 2013 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'; 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
Archived
This topic is now archived and is closed to further replies.