PHP's in_array bevahior surprises me -


this question has answer here:

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

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -