November 22nd, 2006
Is the Array Element in My PHP Code a String or a Constant?
I had a problem come up with two of my clients today. They took code that I had designed on a server running PHP 4.2.2 and moved it to a server running PHP 5.0+. Here’s an example of the type of code I had written.
<? echo $_REQUEST[emailaddress] ?>
This code displays the value the user had entered for emailaddress on a form. On both of the servers running PHP 5.0+, this code was presenting an error message that it had failed to recognize a constant named emailaddress.
The older version of PHP that I was using interprets emailaddress in my code above as the string “emailaddress”. It looks like recent versions of PHP now try to interpret this as a constant. When that fails, it does revert to interpreting emailaddress as a string, but it still outputs an ugly error message.
How should the code look to function as I intended in both 4.2.2 and 5.0+?
<? echo $_REQUEST[“emailaddress”] ?>
Note the quotation marks around emailaddress. In most cases, this is how my code was written already. There were areas where I was referencing elements of arrays inside strings, though, and I had taken this shortcut. Particularly with Fusebox, which I use a lot, there were a few areas in some of my code where putting quotation marks around an array element simply isn’t an option. The strictly typed XML documents in Fusebox don’t give you much leeway.
It seems like PHP comes out with a new minor revision every few weeks, so I’m not exactly sure what exact version number has this change. If any readers out there know any details about this specific upgrade, please sound off in the comments.



