Oct 29, 2001 #1 rhowes IS-IT--Management Joined Jun 6, 2001 Messages 140 Location ZA Hi I have many statements that look like: if($x=="yes" || $x=="no" || $x="maybe" Is ther a more efficient and elegant way of writing the condition? Thanks. Richard
Hi I have many statements that look like: if($x=="yes" || $x=="no" || $x="maybe" Is ther a more efficient and elegant way of writing the condition? Thanks. Richard
Oct 29, 2001 1 #2 rycamor Programmer Joined Jun 3, 1999 Messages 1,426 Location US You could make all the possible positive conditions into an array, with $x being the variable you are checking: Code: <?php $conditions = Array("yes", "no", "maybe"); if(in_array($x, $conditions)) { //etc... } ?> This way, you can easily change your list of conditions, without having to change any other code. See http://www.php.net/in_array. PHP also has many other sophisticated array matching, sorting and comparison functions, so you can extend this approach in many ways. Upvote 0 Downvote
You could make all the possible positive conditions into an array, with $x being the variable you are checking: Code: <?php $conditions = Array("yes", "no", "maybe"); if(in_array($x, $conditions)) { //etc... } ?> This way, you can easily change your list of conditions, without having to change any other code. See http://www.php.net/in_array. PHP also has many other sophisticated array matching, sorting and comparison functions, so you can extend this approach in many ways.