in_array() : Searching presence of an element inside an array

This function will return True or False based on the presence of the search element.
For string, the comparison is done in a case-sensitive manner.
The script below will create an array with some element and then will apply the in_array function to check the presence of an element.
$value=array("Rabin","Reid","Cris","KVJ","John");

if(in_array("Cris",$value)){ echo "Cris is there inside the array";} else{ echo "Cris is not there ";}

Checking PDO driver support using if - else

To check for different database PDO driver support we can use this code.
var_dump(PDO::getAvailableDrivers());
By using PDO::getAvailableDrivers() we can get an array of available PDO drivers, so we can use in_array() to check the presence of SQLite inside the array and if-else to display appropriate message to user.
<?php
if (!in_array("sqlite",PDO::getAvailableDrivers(),TRUE))
{
 throw new PDOException ("Cannot work without a proper database setting up");
}else{
 echo "<font color='green'>Database setup is OK</font>";
}
?>

$strict=TRUE

$a = array('abc','4.56', 4, 1.13);
if (in_array(4.56, $a, true)) {
    echo "4.56  found with strict check";
}else{
	echo "4.56  NOT found with strict check";
}
echo "<BR>";
if (in_array(1.13, $a, true)) {
    echo "1.13  found with strict check";
}else{
	echo "1.13  NOT found with strict check";
}
Output
4.56 NOT found with strict check
1.13 found with strict check
in_array(mixed $needle, array $haystack, bool $strict = false): bool
FunctionDetails
array_search()Search for value inside Array. Returns the key if found, FALSE otherwise
array_keys()Array of keys for matching values, FALSE otherwise
array_key_exists()Search for key inside Array. True if found, FALSE otherwise
Array REFERENCE


Subscribe to our YouTube Channel here



plus2net.com







smo1234

18-06-2012

nice site




PHP video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer