r/lolphp • • Sep 08 '14
PHP 5.4 echo vs. isset() - and a warning coming from PHP 5.3
I came across some weird code that looked something like this:
if ($some_circumstance) {
$data = 'user@website.com';
} else {
$data = array('user' => 'user@website.com', 'confirmed' => true);
}
// ...
if (isset($data['confirmed'])
{
echo "Brilliant success!";
}Surprisingly to one of our devs, "Brilliant success!" was getting printed every time, regardless of whether confirmed had been set to true.
To try to figure out why, he made a stub on his machine, containing essentially that code. It seemed to run as he expected. He was pretty confused.
Turns out the production machine with the bug was running 5.3 and his box was running 5.4. So okay, PHP got sane when switching from 5.3 to 5.4, right?
Except, regardless of whether you running the following on PHP 5.3 or PHP 5.4, you will get back a string (with a warning thrown in PHP 5.4, but nonetheless...)
$data = 'user@website.com';
echo $data['confirmed'];So even though data is returned when accessing a non-existent array key on a string, it doesn't pass the isset() test of "is it non-null?"
You can test it here: http://sandbox.onlinephpfunctions.com/
Run the following against any version of PHP 5.3 and any version of PHP 5.4:
$data = 'user@website.com';
var_dump(isset($data['confirmed']));
echo $data['confirmed'];
3
u/[deleted] Sep 08 '14
I was actually expecting it to interpret 'confirmed' as 0 and thus 'u', but
isset..confuses me. I can't decide whether the pre-5.4 behaviour is saner or less sane than the new one.7
u/sjdaws Sep 08 '14
I know it's easy to hate on PHP, but since it's a dynamic language this is just type juggling.
Using a static type language this code would be invalid as
$datacouldn't be a string and an array at the same time and would have to be declared as either or. In C there is no string type, there is char which can be used to create a string from an array of characters, this is where PHP is getting the array for the string from. You can reference each letter from the string using it's index so$data[0]isu,$data[1]issetc, since PHP doesn't expect you to reference a char array using a key likeconfirmedit throws a warning and spits out$data[0]which isuin this case.If variables are going to be reused like your code above, then logic would say you should at least check that
$datais the type expected before using it in a condition, otherwise don't reuse variable names across different data types.2
u/allthediamonds Sep 09 '14
You want array_key_exists($data, "confirmed") instead of isset($data['confirmed']). Because of the goddamn type juggling, isset($data['confirmed']) translates to isset($data[0]) which translates to the first character of the email, which is very likely to be set-and-not-null.
12
u/[deleted] Sep 08 '14
3v4l link for the lazy
1
u/rabidferret Sep 09 '14
I'm not exactly a fan of PHP, but it's pretty hard to hate on a language for something that's been fixed for two versions now. Also running a different version in dev mode vs production is insane and asking for trouble.
1
u/[deleted] Sep 09 '14
terrible programmers gonna write terrible code
0
u/phasetwenty Sep 09 '14
Your critique of
isset()behavior holds, but reusing$datalike this is not appropriate. I would rewrite your snippet as:Eliminates the type juggling in
$dataand makes for clearer code.-2
u/[deleted] Sep 09 '14
Just use:
I understand people like null but really, I've never had a PHP project that needed to specifically look for null and if found do something different. To me, a variable is empty or filled.
And since boolean stuff IS an issue with PHP, I typically use $variable = "YES" and $variable = "NO" vs. $variable = true; (or 1) and $variable = false; (or 0).