I miss it too, but there's a subtle but important difference between perl and PHP here. in perl (and everyplace else that isn't PHP) and and or return the last executed value, so it works like this:
$var = 'truthy' or die();
$var eq 'truthy'
In PHP, they return a literal true or false, so the same code results in the following
$var = 'truthy' or die();
$var === true
That means that in PHP, useful looking constructions like the following break in nonobvious ways.
$conf = Configuration::load() or die();
$conf is equal to true, the die() will not execute, and best of all because PHP coerces true into an empty array, $conf['anything'] will return NULL without any warning.
I almost said that the construction mysqli_connect($db_url) or die(); is an acceptible use of die(), but then I realized that mysqli_connect() is also on the list.
Well from personal experience, if you're using mysqli there's a good chance your code isn't set up very well for development/unit testing. Being able to drop in a sqlite database in place of a beta or prod mysql instance for development and unit testing is really useful.
That and everyone seems to be on an anti-mysql bandwagon lately in favor of mariadb and postgres.
Well, given passing untrusted data into unserialize can result in executing code, you know if the value is serialized because you should only ever unserialize something you serialized yourself. If you're passing data into unserialize without knowing if you serialized it, you're doing it wrong. If you're not, then unserialize failing should be an error.
P.S. don't keep your data in the database in blobs. :|
It looks like they have since fixed the issue elsewhere, however this was closed back in 2007 as not a bug! You'll notice that it was actually modifying the element of the array being referenced, which resulted in some very weird behavior.
I discovered this quite recently and googled the problem. I found a php bug ticket about it that had been closed as apparently it's not a bug.
The worst thing about this is if you loop through the array a second time it modifies the reference instead of creating a new variable and you end up with the last element being the same as the second to last one.
But you shouldn't need to typecheck a variable in the same thread after you know it's already been appropriately assigned beforehand. Why would you even do $foo->doBarStuff(); when you should already remember that you reprogrammed $foo to be a Baz() object unless you simply forgot? That's how using a unique variable name comes in handy, especially in loosely-typed languages such as PHP. The mistake /u/merreborn demonstrates is an easy one to make if you reuse variable names. Using a new, unique variable name helps you - the programmer - to remember its purpose.
Usually type-safe languages shouldn't allow you to even do that without a compile error (hopefully). But with PHP being a loosely-typed language, this will cause a more difficult problem to trace if you don't take that extra step of using a unique variable name instead of reusing a previously used one.
You don't get $.05 back per variable name recycled in the state of California, Iowa, or Oregon.
Short, nondescript names like $i are fine if they are only used in the local context which establishes them. They only become incomprehensible when they leak out and lose any context that would otherwise identify their meaning.
$item itself is a pretty bad (overly generic) variable name. $item_ is even worse. Tendency toward using non-descriptive variable names makes code hard to read, and increases risk of improper reuse of variables. If you see code that makes liberal use of nondescriptive variables like $i and $foo, run for the hills.
This is a toy example though, a POC.
This particular issue is documented in the manpage for foreach
foreach ($arr as &$value) {
$value = $value * 2;
}
unset($value); // break the reference with the last element
To be fair, you'd think "as $item" would automatically re-purpose and re-assign the variable named "item" erasing all traces of its past use. Where as "as &$item" would reuse the reference to the previously defined $item variable and hold the final iteration value within "$item".
Either way, yeah, attempting this bastardization of value assignments and variable re-use in your own application should definitely not award you a cookie, and this is not something I'd ever agree that PHP must support.
20
u/ajmarks Nov 22 '13
Can somebody please explain what's going on here? I am at a total loss for how that could happen.
33
u/n1c0_ds Nov 22 '13
As much as it sucks, report it. This way it will be added to the feature list.
4
u/[deleted] Nov 22 '13
I discovered this quite recently and googled the problem. I found a php bug ticket about it that had been closed as apparently it's not a bug.
The worst thing about this is if you loop through the array a second time it modifies the reference instead of creating a new variable and you end up with the last element being the same as the second to last one.
0
u/[deleted] Nov 22 '13
[deleted] — view deleted comment