How to check if a value exists in an array in PHP

Topic: PHP / MySQL« Prev|Next »

Answer: Use the PHP in_array() function

You can use the PHP in_array() function to test whether a value exists in an array or not.

  • <?php
  • $zoo = array("Lion", "Elephant", "Tiger", "Zebra", "Rhino", "Bear");
  • if(in_array("Elephant", $zoo)){
  •     echo "The elephant was found in the zoo.";
  • }
  • echo "<br>";
  • if(in_array("Tiger", $zoo)){
  •     echo "The tiger was found in the zoo.";
  • }
  • ?>

Related FAQ

Here are some more FAQ related to this topic:

 
Close

Your Feedback:

 

We would love to hear from you! Please say something.