How to check if string exist in array in PHP

2 Answers

0 votes
// bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

$arr = array("C", "PHP", "C#", "JAVA");

if (in_array("PHP", $arr))
    echo "Exist"; // Exist
else
    echo "NOT Exist";
    
echo "<br>";
$java = in_array("JAVA", $arr);
if ($java)
    echo "Exist"; // Exist
else
    echo "NOT Exist";
    
echo "<br>";
if (in_array("Visual Basic", $arr))
    echo "Exist";
else
    echo "NOT Exist"; // NOT Exist


/*
run:

Exist
Exist
NOT Exist  
   
*/
   

    


answered Jun 25, 2014 by avibootz
edited Jun 29, 2016 by avibootz
0 votes
// bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )

// strict - If the third parameter strict is set to TRUE then 
//          in_array() will check the types of the needle in the haystack.

$a = array('3.14', 13.9, 78.5);

if (in_array('3.14', $a, true))
    echo "3.14 exist with strict <br />";
else
    echo "3.14 Not exist <br />";

if (in_array(13, $a, true)) 
    echo "13.9 exist with strict <br />";
else
    echo "13.9 Not exist <br />";

/*
run:

3.14 exist with strict 
13.9 Not exist 

*/

 



answered Jun 29, 2016 by avibootz

Related questions

3 answers 317 views
1 answer 175 views
175 views asked May 23, 2018 by avibootz
1 answer 202 views
202 views asked Jul 7, 2014 by avibootz
1 answer 174 views
...