PHP in_array() Function

PHP in_array() Function is used to check that a value exists in an array. An in_array() function is built-in function in PHP.

Syntax :

in_array(mixed $search, array $array, bool $strict = false);

Parameter,

$search : Required. It is value for searching in array.

$array : Required. It is input array.

$strict : Optional. Default value is false. If it is true then the in_array() function will also check the data types of the search value in an array.

Return Values :

It returns true if searched value is found in the array otherwise false.

Let's see below example to understand php in_array() Function in details.

Example :

<?php
echo "<br><br> Check value exists in array where strict is false.";
$tutorials = array("JAVA", ".NET", "Wordpress", "Centos","Drupal");
if (in_array("Centos", $tutorials)) {
echo "<br>It is available";
}else{
echo "<br>It is not available";
}

if (in_array("PHP", $tutorials)) {
echo "<br>It is available";
}else{
echo "<br>It is not available";
}
echo "<br><br> Check value exists in array where strict is true.";
$num = array('2.40', 23.7, 3.77);

if (in_array('23.7', $num, true)) {
echo "<br>'23.7' is found with strict check\n";
}

if (in_array(2.40, $num, true)) {
echo "<br>2.40 is found with strict check\n";
}
if (in_array(3.77, $num, true)) {
echo "<br>3.77 is found with strict check\n";
}
?>

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

62323