0

I have a numeric variable $i. This is a while loop, and $i increments after each iteration.

How can a conditional statement be made as to not be necessary to write such a long statement if($i == 1 || $i == 2 || $i == 25 [...])?

Thanks in advance!

6
  • 1
    Are the values arbitrary, or is their inclusion governed by a mathematical equation? Commented Jul 9, 2013 at 3:35
  • 1
    create an array not a string then use in_array() Commented Jul 9, 2013 at 3:36
  • 1
    Adeneo's answer included very similar logic to Sumoanand's: both said to use in_array(). Sumoanand posted his first. Commented Jul 9, 2013 at 3:41
  • 1
    actully i was first :-) Commented Jul 9, 2013 at 3:44
  • 1
    thank you, i just want to be loved :-) Commented Jul 9, 2013 at 3:46

2 Answers 2

3

Use php in_array.

$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
    echo "Got Irix";
}
Sign up to request clarification or add additional context in comments.

Comments

0

Seems a bit obvious that you should take a deeper look at the documentation of PHP. There is standards functions that can do a lot and help you save a lot of time.

You say that you've got a string but show an example with an array. Assuming you really have a string in input you may try explode() function to explode it to an array of objects.

Then having a real Array in_array() function will do the job.

$resultString = "12 13 17 26";
$resultArray = explode(" ", $resultString);
if (in_array("13", $resultArray)) {
    echo "found";
} else {
    echo "not found";
}

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.