Skip to content

Instantly share code, notes, and snippets.

@janich
Created March 15, 2013 08:16
Show Gist options
  • Save janich/5168262 to your computer and use it in GitHub Desktop.
Save janich/5168262 to your computer and use it in GitHub Desktop.
#FridayFun Can you figure this out? :)
<?php
/*
* #FridayFun
* Can you figure this out :)
*/
header("Content-type: text/plain; Charset=UTF-8");
$str = "e903916d20ad926f38e239fbf0ba1923";
$test = (int) $str;
echo "We have a string id:\n";
var_dump($str);
echo "\nAnd when we cast it to (int), it becomes:\n";
var_dump($test);
echo "\nNow lets see if they are the same ($str == $test):\n";
if ($str == $test) {
echo "- Yes, they are the same!!"; // Wtf?
} else {
echo "- No, they're not the same.";
}
@zanardigit
Copy link

What's the question exactly?

If you want them to differ, you should check with if ($str === $test).

Else you are comparing a string with an integer:

string == integer

becomes

(int) string == integer

and of course that's true.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment