slyte33 Posted December 3, 2009 Share Posted December 3, 2009 Basically the title says it, but I know there is something with PHP easier than a bunch of lines a code than this: If ($example == 1) { }else if ($example == 2) { } I know you can make a function to do things easier, but that's selecting something from the DB and using loops. Just a nice simple if/else if statement, is there an easier way, like 1 line of code or something? It's a noob question, but I am just curious, thanks Link to comment https://forums.phpfreaks.com/topic/183887-easier-and-more-organized-way-than-ifelse-if/ Share on other sites More sharing options...
mikesta707 Posted December 3, 2009 Share Posted December 3, 2009 switch($example){ case 1: //stuff break; case 2: //stuff break; } Its called a switch/case statement Link to comment https://forums.phpfreaks.com/topic/183887-easier-and-more-organized-way-than-ifelse-if/#findComment-970718 Share on other sites More sharing options...
slyte33 Posted December 3, 2009 Author Share Posted December 3, 2009 switch($example){ case 1: //stuff break; case 2: //stuff break; } Its called a switch/case statement Thanks With google I also found another way: $example = (($example2) > 0)?([dosomething?):dosomething?; How does that work above? Link to comment https://forums.phpfreaks.com/topic/183887-easier-and-more-organized-way-than-ifelse-if/#findComment-970731 Share on other sites More sharing options...
premiso Posted December 3, 2009 Share Posted December 3, 2009 That is called the ternary operators (? which is just a short if / else statement: $example = ($example2 > 0)?'Greater Than 0!':'Less Than 0!'; echo $example; So basically if example2 is greater than 0 set example to be "Greater Than 0!" else, set it to be "Less than 0!". You cannot do anything more complex then an if/else with the ternary operator. Link to comment https://forums.phpfreaks.com/topic/183887-easier-and-more-organized-way-than-ifelse-if/#findComment-970735 Share on other sites More sharing options...
Maq Posted December 3, 2009 Share Posted December 3, 2009 *Less than or equal to 0 Link to comment https://forums.phpfreaks.com/topic/183887-easier-and-more-organized-way-than-ifelse-if/#findComment-970738 Share on other sites More sharing options...
roopurt18 Posted December 3, 2009 Share Posted December 3, 2009 You're usually best off just using the if..else if...else if...else if...else syntax. Link to comment https://forums.phpfreaks.com/topic/183887-easier-and-more-organized-way-than-ifelse-if/#findComment-970741 Share on other sites More sharing options...
slyte33 Posted December 3, 2009 Author Share Posted December 3, 2009 Alright thank you everyone Link to comment https://forums.phpfreaks.com/topic/183887-easier-and-more-organized-way-than-ifelse-if/#findComment-970742 Share on other sites More sharing options...
premiso Posted December 3, 2009 Share Posted December 3, 2009 *Less than or equal to 0 Pshhh that is crazy talk. Link to comment https://forums.phpfreaks.com/topic/183887-easier-and-more-organized-way-than-ifelse-if/#findComment-970756 Share on other sites More sharing options...
Maq Posted December 3, 2009 Share Posted December 3, 2009 *Less than or equal to 0 Pshhh that is crazy talk. Don't hate on the 0... Link to comment https://forums.phpfreaks.com/topic/183887-easier-and-more-organized-way-than-ifelse-if/#findComment-970757 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.