AniqueAli Posted October 26, 2014 Share Posted October 26, 2014 <?php$id1 = '5' ;$id = array(1,2,3,4,5,6,7); foreach($id as $value){if($id1==$value){ echo 'One of them matches, ' ; echo 'Do not execute command <BR>' ;} ////// if ends here} ////// loop ends here?> How to do something if all statements are false ? like when id $id1 = '10' , ? Quote Link to comment https://forums.phpfreaks.com/topic/292075-php-of-one-php-condition-is-true/ Share on other sites More sharing options...
boompa Posted October 26, 2014 Share Posted October 26, 2014 (edited) You could just use in_array here. <?php $id1 = '5'; $id = array(1,2,3,4,5,6,7); if (in_array($id1, $id)) { // Found match } else { // No match } Edited October 26, 2014 by boompa Quote Link to comment https://forums.phpfreaks.com/topic/292075-php-of-one-php-condition-is-true/#findComment-1494816 Share on other sites More sharing options...
jcbones Posted October 26, 2014 Share Posted October 26, 2014 Or, if you want something to run if it isn't in the array. if(!in_array($id1,$id)) //if NOT in array. Quote Link to comment https://forums.phpfreaks.com/topic/292075-php-of-one-php-condition-is-true/#findComment-1494817 Share on other sites More sharing options...
Barand Posted October 27, 2014 Share Posted October 27, 2014 However, if you do need to loop and process all the values in the array then you can check this way <?php $id1 = '5' ; $id = array(1,2,3,4,5,6,7); $found = 0; // set a flag variable to FALSE foreach($id as $value){ if($id1==$value){ echo 'One of them matches, ' ; echo 'Do not execute command <BR>' ; $found = 1; // set the flag to TRUE if found } } if (!$found) { // use flag to see if it was found // do 'not found' action } ?> Quote Link to comment https://forums.phpfreaks.com/topic/292075-php-of-one-php-condition-is-true/#findComment-1494922 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.