tags:

views:

42

answers:

3

Is there a specific way to check for a specific integer within a switch statment.

For example.

$user = $ads[$i]->from_user;

To check for the number 2 as $i in the above expression.

+2  A: 

You can check like:

if ($ads[$i] === 2)
{
 // code here
}

Or if you meant alone, you can do:

if ($i === 2)
{
 // code here
}

If the number in string representation (type), you should use == rather than ===.

If however you meant whether 2 is present in the array $ads:

if (in_array(2, $ads))
{
 // 2 found in $ads array
}
Web Logic
+1  A: 

If i understood you right, what you want is to check if the key2exists in$ads.

if(array_key_exists(2, $ads)) {
    // the key 2 exists in the array
}

This way, you should get the result in constant time O(1) becausearray_key_existsis implemented with a hashtable lookup.

in_arraywould require linear time O(n).

abenthy
Was this what you meant or not?
abenthy
A: 

its simple, better you can use === operator to match your values with its datatype

VAC-Prabhu