Difference between is_array() and in_array() in PHP.

 





is_array():

is_array() fucntion used to check whether the variable is array or not. If it is an array, it simply return 1(true) otherwise it returns nothing. Let's see a example below:

<?php
$x = "World";
echo "x is " . is_array($x) . "<br>"; //x will return nothing,cause not an array

$y = array("earth", "ocean", "desert");
echo "y is " . is_array($y) . "<br>"; //y will return 1,cause it is an array
?>

The result will be: x is
                                  y is 1

in_array():

in_array() fucntion used to check whether the specific value is in array or not. The search parameter is case sensitive.  Let's see a example below:

<?php
$flower = array("Lili", "Cosmos", "Sunflower", "Orchid", "Rose");

if (in_array("Orchid", $flower )) //'Orchid' is case sensitive, you can not use 'orchid'
  {
  echo "This flower found in flower array!";
  }
else
  {
  echo "Not found!!!";
  }
?>

The result will be:  This flower found in flower array!

Post a Comment (0)
Previous Post Next Post