PHP - Comparing multiple values

This is a very practical example. This example shows you how to take a value and compare it against a set of values in an array.
The example shows two ways to accomplish this. The easy way and even easier way.

Here is the example:

<?php
$entered_value = $_GET["processor"];
$proc_array = array("P4", "P4 Celeron", "P3", "P2", "P2 Celeron", "P1");
// easy
for ($i = 0; $i < count($proc_array); $i++)
  if ($entered_value == $proc_array[$i])
  {
    echo "Yes, your processor is PENTIUM family";
    break;
  }
// easier
if (in_array($entered_value, $proc_array))
   echo "Yes, your processor is PENTIUM family";
?>

Leave a Reply