Skip to content

Instantly share code, notes, and snippets.

@timotheemoulin
Last active October 15, 2020 06:13
Show Gist options
  • Save timotheemoulin/02013b006b701d1a684ded46aab06f4c to your computer and use it in GitHub Desktop.
Save timotheemoulin/02013b006b701d1a684ded46aab06f4c to your computer and use it in GitHub Desktop.
Does PHP evaluate things the way you thought?
<?php
// counting values
var_dump(count(array())); // 0
var_dump(count(false)); // 1
var_dump(count(true)); // 1
var_dump(count(null)); // 0
var_dump(count(1)); // 1
var_dump(count(0)); // 1
var_dump(count('asdf')); // 1
// checking array values (in_array)
var_dump(in_array(true, ['foo', 'bar', ])); // true
var_dump(in_array(1, [true])); // true
var_dump(in_array(1, [true]), true); // false (use the $strict parameter)
// substracting floats
var_dump(1 - 0.99); //float(0.01)
var_dump(10 - 9.99); //float(0.0099999999999998)
var_dump(10 - 10.01); //float(-0.0099999999999998)
// casting function return values
function returnbool():bool{
return -1;
}
function returnint():int{
return false;
}
var_dump(returnbool()); // bool(true)
var_dump(returnint()); // int(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment