$number = 1,3,5,6,8,9
Posted: Fri Jul 21, 2006 2:51 pm
Is there a way to write out an if statement where $numbers= any of these numbers 1 2 4 5 6 8 9 with out having to do $numbers=1 || $numbers=2 ...
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
if (is_numeric($numbers))Code: Select all
if (is_numeric($numbers) && $numbers >=0 && $numbers <= 9)tecktalkcm0391 wrote:Is there a way to write out an if statement where $numbers= any of these numbers 1 2 4 5 6 8 9 with out having to do $numbers=1 || $numbers=2 ...
Code: Select all
$number = 1;
switch ($number) {
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
echo $number;
break;
default:
echo 'not one of the numbers';
break;
}
in_array() is most appropriate thoughCode: Select all
if ($number >=0 && $number <= 9)Your code would return true for the numbers 3 and 7.Ward wrote:The switch is overkill, when you can do it in one line with my method. Since PHP doesnt have strong data types, you actually don't even need the is_numeric part, this would actually work:Code: Select all
if ($number >=0 && $number <= 9)
Code: Select all
<?php
$num = 5;
$array = array(1, 2, 4, 5, 6, 8, 9 );
if(in_array($num, $array)) echo $num . " is one of the numbers";
?>Yeah, I didnt notice that 3 and 7 weren't included in the matches. My bad, I misread. Thats what running on 4 hours of sleep a night will do to youastions wrote:Your code would return true for the numbers 3 and 7.Ward wrote:The switch is overkill, when you can do it in one line with my method. Since PHP doesnt have strong data types, you actually don't even need the is_numeric part, this would actually work:Code: Select all
if ($number >=0 && $number <= 9)