Home > Backend Development > PHP Tutorial > in_array() definition and usage_PHP tutorial

in_array() definition and usage_PHP tutorial

WBOY
Release: 2016-07-13 17:12:54
Original
1265 people have browsed it

A basic tutorial for getting started with PHP. Friends in need can refer to it on how to use the in_array function.

bool in_array ( mixed $needle , array $haystack [, bool $strict ] )
Searches the haystack for needle and returns TRUE if found, FALSE otherwise.

If the value of the third parameter strict is TRUE, the in_array() function will also check whether the type of needle is the same as that in haystack.

Note:

If needle is a string, the comparison is case-sensitive.


Note:

Prior to PHP version 4.2.0, needle was not allowed to be an array.


Note: If the value parameter is a string and the type parameter is set to true, the search is case-sensitive.

Example #1 in_array() Example

The code is as follows
 代码如下 复制代码
$os = array("Mac", "NT", "Irix", "Linux");
if (in_array("Irix", $os)) {
echo "Got Irix";
}
if (in_array("mac", $os)) {
echo "Got mac";
}
?>
Copy code

$os = array("Mac", "NT", "Irix", "Linux");

if (in_array("Irix", $os)) {

echo "Got Irix";
}

if (in_array("mac", $os)) {
代码如下 复制代码

$a = array('1.10', 12.4, 1.13);

if (in_array('12.4', $a, true)) {
echo "'12.4' found with strict checkn";
}
if (in_array(1.13, $a, true)) {
echo "1.13 found with strict checkn";
}
?>

echo "Got mac";

}
?>




The second condition fails because in_array() is case sensitive, so the above program reads:

Got Irix


Example #2 in_array() strict type checking example

$a = array('1.10', 12.4, 1.13);

if (in_array('12.4', $a, true)) {

echo "'12.4' found with strict checkn"; if (in_array(1.13, $a, true)) { echo "1.13 found with strict checkn"; } ?> Note: in_array function returns 0,1 When converted to boolean, the following values ​​are considered FALSE: Boolean value FALSE Integer value 0 (zero)
Floating point value 0.0 (zero)
Blank string and string "0" Array without member variables Object without units Special type NULL (including variables that have not been set) All other values ​​are considered TRUE (including any resources). Warning -1, like other non-zero values ​​(positive or negative), is considered TRUE http://www.bkjia.com/PHPjc/629252.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629252.htmlTechArticleA basic tutorial for getting started with PHP. Regarding how to use the in_array function, friends in need can refer to it. bool in_array ( mixed $needle , array $haystack [, bool $strict ] ) in h...
The code is as follows
Copy code
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template