Last active
December 24, 2015 22:59
-
-
Save datacodesolutions/6876641 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Case 1 Produces: | |
// int_of_value: 0 is equal to value: dog | |
$value = 'dog'; | |
$int_of_value = (int) $value; | |
if ($int_of_value == $value) { | |
/* | |
* http://php.net/manual/en/language.operators.comparison.php | |
* If you compare a number with a string or the comparison involves numerical strings, then each string is | |
* converted to a number and the comparison performed numerically. | |
*/ | |
// in this case 'dog' would be converted to an integer when comparing | |
echo 'int_of_value: ' . $int_of_value . ' is equal to value: ' . $value; | |
} else { | |
echo 'int_of_value: ' . $int_of_value . ' is NOT equal to value: ' . $value; | |
} | |
// Case 2 Produces: | |
// int_of_value: 0 is NOT equal to value: dog | |
$value = 'dog'; | |
$int_of_value = (int) $value; | |
if ($int_of_value === $value) { | |
echo 'int_of_value: ' . $int_of_value . ' is equal to value: ' . $value; | |
} else { | |
// strict comparison means string and integer are not equal | |
echo 'int_of_value: ' . $int_of_value . ' is NOT equal to value: ' . $value; | |
} | |
// Case 3 Produces: | |
// int_of_value: 0 is NOT equal to value: dog | |
$value = '0'; | |
$int_of_value = (int) $value; | |
if ($int_of_value === $value) { | |
echo 'int_of_value: ' . $int_of_value . ' is equal to value: ' . $value; | |
} else { | |
// strict comparison means string and integer are not equal | |
echo 'int_of_value: ' . $int_of_value . ' is NOT equal to value: ' . $value; | |
} | |
// Case 4 Produces: | |
// int_of_value: 0 is equal to value: 0 | |
$value = 0; | |
$int_of_value = (int) $value; | |
if ($int_of_value === $value) { | |
// two integers with same value - strict comparison succeeds | |
echo 'int_of_value: ' . $int_of_value . ' is equal to value: ' . $value; | |
} else { | |
echo 'int_of_value: ' . $int_of_value . ' is NOT equal to value: ' . $value; | |
} | |
// Case 5 Produces: | |
// no | |
$value = '0'; | |
if (is_integer($value)) { | |
echo 'no'; | |
} else { | |
// guessing the 'yes' and 'no' are meant to be switched here? | |
// is_integer fails because '0' is a string. to test for more general numeric use "is_numeric" | |
// http://www.php.net/manual/en/function.is-int.php | |
echo 'yes'; | |
} | |
// Case 6 Produces: | |
// yes | |
$value = 0; | |
if (is_integer($value)) { | |
// once again, guessing 'yes' and 'no' are switched | |
// is integer succeeds because 0 is an integer. | |
echo 'no'; | |
} else { | |
echo 'yes'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What ended up working was ctype -- otherwise -- PHP treats the value as a string. Crazy.
Thanks for your helP!