note 37266 modified in function.in-array by vincent

From: Date: Fri, 07 Nov 2003 22:08:42 +0000
Subject: note 37266 modified in function.in-array by vincent
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-60129@lists.php.net to get a copy of this message
If you're trying to compare a variable against several possible other variables, like this... if ($var == 1 || $var == 2 || $var == 3 || $var == 4) { // do this } You can make your code a lot neater by doing this: if (in_array($var,array(1,2,3,4))) { // do this } This way, you're not repeating the "$var == x ||" part of your expression over and over again. If you've used MySQL's IN() function for searching on multiple values, you'll probably appreciate using this code in your PHP scripts. Hope this helps a few people. --was-- If you're trying to compare a variable against several possible other variables, like this... if ($var = 1 || $var = 2 || $var = 3 || $var = 4) { // do this } You can make your code a lot neater by doing this: if (in_array($var,array(1,2,3,4))) { // do this } This way, you're not repeating the "$var = x ||" part of your expression over and over again. If you've used MySQL's IN() function for searching on multiple values, you'll probably appreciate using this code in your PHP scripts. Hope this helps a few people. http://www.php.net/manual/en/function.in-array.php

« previous php.notes (#60129) next »