PHP's in_array bevahior surprises me -
this question has answer here:
- a problem in_array 3 answers
- php's variable type leniency 3 answers
while validating dynamic saving method in controller wanted make sure given case accepts 0 , 1 valid values. when tried manipulate input form, submitting 'aaa' value following still returns true. why that?
var_dump(in_array('aaa', [0, 1])); // true, expecting return false
you need use "strict" setting, force function check types of elements well:
var_dump(in_array('aaa', [0, 1], true));
http://php.net/manual/en/function.in-array.php states
if third parameter strict set true in_array() function check types of needle in haystack.
the reason returns true because string truthy, , 1
.
if( "aaa" ){ echo "you see me"; }
Comments
Post a Comment