Search code examples
phpstrpos

Checking for multiple strpos values


I am wondering how to complete multiple strpos checks.

Let me clarify:
I want strpos to check the variable "COLOR" to see if any numbers from 1 to 8 are anywhere in the variable. If any numbers from 1 to 8 are present, I want to echo "selected".

Examples:
Let's say only the number 1 is in the variable, it will echo "selected".
Let's say the numbers 1 2 and 3 are in the variable, it will echo "selected."
Let's say the numbers 3 9 25 are in the variable, it will echo "selected" (because of that 3!!).
Let's say only the number 9 is in the variable, it will NOT echo.
Let's say the numbers 9 25 48 are in the variable, it will NOT echo.


Solution

  • I just used the OR statement (||)

    <?php 
      if ((strpos($color,'1') || strpos($color,'2') || strpos($color,'3')) === true) 
       {
          //do nothing
       } else { 
          echo "checked"; 
       } 
    ?>