in_array()
8th February 2021
0 Comments
Programming Language
PHP [Versions: 4, 5, 7, 8]
Syntax
Typical Usage
Checks an array to determine if a value exists.
Accepted Parameters
- $needle (mixed) (Required): The value to look for.
- $haystack (array) (Required): The array to search.
- $strict (boolean) (Optional) (Default = false): If set to true, data types will also be compared. ie, the integer 1 will be seen as different from the string “1” .
Return Value(s)
Returns true or false depending on whether or not the requested value exists.Notes
Important:
If $needle is a string, the comparison is done in a case-sensitive manner.
Examples
Example 1
<?php
$pets_array = ['dogs', 'cats', 'birds', 'fish'];
if(in_array('cats', $pets_array)){
echo 'Yes, she has cats.';
}
else {
echo 'No, she doesn't have a single cat.';
}
The above will output “Yes, she has cats.” because ‘cats’ is in the array.
Was This Helpful?
If this post has helped you, please let me know. Your feedback will be highly appreciated and will help me build better content.