Search code examples
phparrayscomparisonstring-comparisoncomparison-operators

Comparing 1 value to single array in PHP


How to comparing one value to every single value in the particular array?

For example:

$list =  ("blue", "red", "green", "yellow", "orange", "white");
$value = "blue";

if ($value == $list)
{
  // then print "This is BLUE";
}

I need only one time to determine the colors, no need repeating to print other color.

This may bring me to implement a dynamic value retrieved from database, and then comparing a [hardcode] array in PHP script. Then, return the exact value match onto the screen, example as above scenario.


Solution

  • Could you be looking for in_array()?

    if (in_array('blue', $colors)) {
        // the color blue is there
    }