$number = 1,3,5,6,8,9

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

$number = 1,3,5,6,8,9

Post by tecktalkcm0391 »

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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Ward
Forum Commoner
Posts: 74
Joined: Thu Jul 13, 2006 10:01 am

Post by Ward »

If you're just trying to find if a value is numeric, you can use:

Code: Select all

if (is_numeric($numbers))
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) && $numbers >=0 && $numbers <= 9)
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Re: $number = 1,3,5,6,8,9

Post by jmut »

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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

regular expressions are overkill in this case I would say.
User avatar
MrPotatoes
Forum Regular
Posts: 617
Joined: Wed May 24, 2006 6:42 am

Post by MrPotatoes »

so would be doing a math equation to figure it out. just do the switch statement like above
Ward
Forum Commoner
Posts: 74
Joined: Thu Jul 13, 2006 10:01 am

Post by Ward »

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)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

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)
Your code would return true for the numbers 3 and 7.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

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";
?>
Ward
Forum Commoner
Posts: 74
Joined: Thu Jul 13, 2006 10:01 am

Post by Ward »

astions wrote:
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)
Your code would return true for the numbers 3 and 7.
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 you :P
Post Reply