$number = 1,3,5,6,8,9
Moderator: General Moderators
- tecktalkcm0391
- DevNet Resident
- Posts: 1030
- Joined: Fri May 26, 2006 9:25 am
- Location: Florida
$number = 1,3,5,6,8,9
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 ...
Last edited by tecktalkcm0391 on Fri Jul 21, 2006 3:08 pm, edited 1 time in total.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
array_search() or in_array() perhaps.
If you're just trying to find if a value is numeric, you can use:
However if you're trying to find if a value is numeric AND beween 0 and 9, then
Code: Select all
if (is_numeric($numbers))Code: Select all
if (is_numeric($numbers) && $numbers >=0 && $numbers <= 9)Re: $number = 1,3,5,6,8,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 though- MrPotatoes
- Forum Regular
- Posts: 617
- Joined: Wed May 24, 2006 6:42 am
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)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)