In_array () Definition and usage

Source: Internet
Author: User

For more information about how to use the in_array function, see the basic tutorial for getting started with php.

Bool in_array (mixed $ needle, array $ haystack [, bool $ strict])
Search for needle in haystack. If yes, TRUE is returned. Otherwise, FALSE is returned.

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

Note:

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


Note:

Before PHP version 4.2.0, needle cannot be an array.

 


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

Example #1 in_array ()

The Code is as follows: Copy code
<? Php
$ OS = array ("Mac", "NT", "Irix", "Linux ");
If (in_array ("Irix", $ OS )){
Echo "Got Irix ";
}
If (in_array ("mac", $ OS )){
Echo "Got mac ";
}
?>

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

Got Irix


Example #2 in_array () Strict type check Example

The Code is as follows: Copy code

<? Php
$ A = array ('1', 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: The in_array function returns 0, 1

When converted to boolean, the following values are considered FALSE:
Boolean value FALSE
Integer value 0 (0)
Floating point value: 0.0 (0)
Blank string and string "0"
Array without member variables
Objects without cells
Special Type NULL (including unset variables)
All other values are considered to be TRUE (including any resources ).
Warning
-1 is considered TRUE, like other non-zero values (both positive and negative values ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.