Jump to content


Photo

setting an if condition on arrays....


  • Please log in to reply
1 reply to this topic

#1 mkosmosports

mkosmosports
  • Members
  • PipPipPip
  • Advanced Member
  • 196 posts

Posted 30 October 2006 - 03:57 AM

Hey.

I want to set an if condition on a group of numbers...

$group=$_GET["group"]; 
$num = array("1","2","3","4","5","6","7","8","9","10");

if ($group != $num) //If the number represented by the group parameter is not among the numbers in the $num array
{
echo("sorry");
}
How come the above does not work for me?

Upfront thanks to anyone who can help me out..

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 30 October 2006 - 04:30 AM

That doesn't work since you're comparing a value to an array. You want to use the function in_array()

<?php
$group=$_GET["group"]; 
$num = array("1","2","3","4","5","6","7","8","9","10");

if (!in_array($group,$num)) //If the number represented by the group parameter is not among the numbers in the $num array
{
    echo("sorry");
}?>

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users