0

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.

1
  • 1
    Your question title is confusing (after reading the actual question). Commented May 22, 2010 at 13:12

3 Answers 3

2

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
}
Sign up to request clarification or add additional context in comments.

Comments

1

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).

1 Comment

Was this what you meant or not?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.