PHP in_array() / array_search() odd behaviour
This behaviour of the function in_array()
and array_search()
is not a bug, but instead well documented behaviour.
Both functions have a 3rd optional parameter called $strict
which by default is FALSE:
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
mixed array_search ( mixed $needle , array $haystack [, bool $strict = false ] )
Now what that means is that by default both functions use loosely(==
) comparison to compare the values. So they only check if the values are the same after PHP type juggling and without checking the type. Because of that in your example TRUE == "any none emtpy string"
evaluates to TRUE.
So by setting the 3rd parameter to TRUE while calling the function you say that PHP should use strict(===
) comparison and it should check value AND type of the values while comparing.
See this as a reference: How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
Weird in_array() behaviour
Use third parameter of in_array
to force strict match
<?php
$_aOperatorsOneOptin = array('DE-010', 'DE-005');
$bMatchPaymentOperator = in_array(0 , $_aOperatorsOneOptin, true);
if($bMatchPaymentOperator == true)
echo 'found';
Weird in_array() behaviour
Use third parameter of in_array
to force strict match
<?php
$_aOperatorsOneOptin = array('DE-010', 'DE-005');
$bMatchPaymentOperator = in_array(0 , $_aOperatorsOneOptin, true);
if($bMatchPaymentOperator == true)
echo 'found';
Weird in_array() behaviour
Use third parameter of in_array
to force strict match
<?php
$_aOperatorsOneOptin = array('DE-010', 'DE-005');
$bMatchPaymentOperator = in_array(0 , $_aOperatorsOneOptin, true);
if($bMatchPaymentOperator == true)
echo 'found';
Weird array_search behaviour
This is because array_search
uses ==
to compare things. This makes PHP convert the operands so that their types match.
1 == "I want to find this text"
"aa" == "I want to find this text"
0 == "I want to find this text"
In the 1st and 3rd ones, PHP needs to convert "I want to find this text"
to a number so it can compare. When converting a string to a number, PHP reads from the beginning of the string and stops at the first non-numeric character. So "I want to find this text"
is converted to 0
.
So the comparisons made are
1 == "I want to find this text" => 1 == 0 => false
"aa" == "I want to find this text" => false
0 == "I want to find this text" => 0 == 0 => true
And, that's why you get 2.
To fix this, do this: array_search("I want to find this text", $ray, true)
The 3rd parameter tells array_search
to use ===
instead. This does not convert types, instead it compares them too. That will give you FALSE
since nothing matches "I want to find this text"
in both type and value.
Unexpected in_array() behaviour
The function in_array()
by default performs a loose comparison and this causes PHP to evaluate the expression (0 == "article")
as TRUE
.
You need to use the strict parameter for in_array()
to also check the types of the needle in the haystack:
var_dump( in_array('article', $enabled, true) );
Demo!
in_array() returns wrong values?
in_array
you have to set type
Syntax:
in_array(search,array,type);
var_dump(in_array(0, array('a', 'ab', 'abc'), true));
Note: If the search parameter is a string and the type parameter is set to TRUE, the search is case-sensitive.
http://php.net/manual/en/function.in-array.php
in_array returns True from a value that not exists
in_array()
check for the values of the arrays.
If you are setting a value as true
, it will return true
because of that, unless, as mentioned by @AymDev before, you set the third parameter to be strict.
Related Topics
Force Ssl/Https Using .Htaccess and Mod_Rewrite
Increasing the Maximum Post Size
How to Connect to Multiple MySQL Databases on a Single Webpage
How to Convert Date to Timestamp in PHP
How to Send 100,000 Emails Weekly
How to Replace "If" Statement With a Ternary Operator ( : )
What in Layman'S Terms Is a Recursive Function Using PHP
Do Htmlspecialchars and MySQL_Real_Escape_String Keep My PHP Code Safe from Injection
Convert Base64 String to an Image File
Forcing to Download a File Using PHP
How to Use Store and Use Session Variables Across Pages
MySQLi_Query() Expects At Least 2 Parameters, 1 Given
Simplest Way to Detect a Mobile Device in PHP
Sending Email With Gmail Smtp With Codeigniter Email Library
Property [Title] Does Not Exist on This Collection Instance