-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|569|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
SoCoder -> Blogs Home -> Blogs

Created : 04 February 2011

PHP being weird!

So I was busy coding away at work and I ran in to the most weird PHP issue I've ever had.

So here was the problem from my perspective. I was creating a script that writes out records into a PDF. For some strange reason, the last two rows were identical. I scratched my head and thought maybe the database had duplicates. Nope. Maybe my query is wrong? Nope. Maybe I have some funky logic that is doing that. Sorta.

Like a lot of languages, PHP lets you assign a reference to a variable:

This causes some very interest problems when you reuse the reference in a foreach() loop:

(Please ignore the obvious way to simplify this code. It's just a very simple example.)

The reference will cause the pointer inside of $value to be carried over to the second foreach loop. This results in quirky problems.

The solution is pretty simple. Just unset $value:

It's not technically a bug so the PHP developers wont fix it. The expected behavior of the foreach() loop is to reset the pointer, so this issue will trip a lot of people up. It's not documented anywhere either.



Friday, 04 February 2011, 20:05
This is one of the reasons why you shouldn't use pointers because you get strange corner cases like this.
Saturday, 05 February 2011, 02:43
Why would you write records into a PDF? o_O
Saturday, 05 February 2011, 12:18
If you ever want to give someone a document (one for them to keep) then PDF is almost certainly the best format to use.

I imagine that's what Stealth is doing, generating a document for people to keep.
Sunday, 06 February 2011, 00:15
Correct. These are end of the month statement records that businesses will want to keep copies of.
Sunday, 06 February 2011, 03:25
If they wanna keep copies then those copies should be in a DB, not in a PDF.
Sunday, 06 February 2011, 04:44
PDFs are how companies operate.
It's just the way it is.
Although we all know Fax machines are completely feckin' useless, they will still be used by 10,000,000,000 businesses each and every day.
A PDF is a software version of a fax, and will be inexplicably used until after we're all long and dead.

It's just the way it is.
No point in arguing about it.
Sunday, 06 February 2011, 07:51
Afr0 If they wanna keep copies then those copies should be in a DB, not in a PDF.
You can't e-mail someone a DB entry, or offer it as a download (well you can but it's not very user intuitive). You can with a PDF.
Sunday, 06 February 2011, 22:13
It also could potentially be printed. So PDFs are a good choice.
Monday, 07 February 2011, 08:26
Evil Roy Ferguso
I think this is another good argument for lexical scope.