0

Basically I have this:

<?php
    $variable = 8;
    if ( $variable == array(5,6,7) ) :
    echo '<p>'.$variable.'</p>;
    endif;
?>

Can I test if a variable is one of those values like that or do I have to test each individually, like:

<?php
    $variable = 8;
    if ( ( $variable == 5 ) || ( $variable == 6 ) ) :
    echo '<p>'.$variable.'</p>;
    endif;
?>

Thanks.

4 Answers 4

3

I think you're looking for in_array():

if(in_array($variable, array(5,6,7) )){
    // $variable is in the array!
}
Sign up to request clarification or add additional context in comments.

1 Comment

@BiffWebsterJr. You're welcome - glad it worked for you. Please be sure to accept an answer when you get the chance.
3

You can use in_array

if (in_array($variable, array(5,6,7))) {
    // do something
}

Comments

1

Use in_array:

if (in_array($variable, array(5, 6, 7))

Comments

1

use in_array function for this

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.