RE: [PHP] sloppy use of constants as strings. WAS: What does mean?
On Mon, April 30, 2007 9:05 pm, Daevid Vincent wrote: echo EOF BROWSER: $_SERVER[HTTP_USER_AGENT] EOF; Isn't that form (sans quote marks) deprecated and frowned upon? ?php error_reporting( E_ALL ); echo EOF BROWSER: $_SERVER[HTTP_USER_AGENT] EOF; Why would cleaner, perfectly error free code be frowned upon? http://us2.php.net/manual/en/language.types.array.php A key may be either an integer or a string. If a key is the standard representation of an integer, it will be interpreted as such (i.e. 8 will be interpreted as 8, while 08 will be interpreted as 08). I was always under the impression that using: $_SERVER[HTTP_USER_AGENT] or $foo[myindex] Was bad compared to the proper way of: $_SERVER['HTTP_USER_AGENT'] or $foo['myindex'] Those *ARE* bad, but they aren't embedded in double-quotes, nor heredoc, which are entirely different environments. Those are raw PHP syntax strings, with no delimiters to set them off as string Just like it's: define('MYTHING', true); not define(MYTHING, true); Same exact thing. (again, note the ' marks) http://us2.php.net/manual/en/function.define.php http://us2.php.net/manual/en/language.constants.php If you use an undefined constant, PHP assumes that you mean the name of the constant itself, just as if you called it as a string (CONSTANT vs CONSTANT). An error of level E_NOTICE will be issued when this happens. See also the manual entry on why $foo[bar] is wrong (unless you first define() bar as a constant). If you simply want to check if a constant is set, use the defined() function. So, I think you're relying upon a little work that PHP does for you in that an undefined CONSTANT is turned into a STRING i.e. bad. Personally, I *hate* that it does this work, and would love to see a little stricter parsing done and throw a fatal error if you try to use an undefined constant. I'm *NOT* relying on that at all. When it is inside of the quotes or heredoc, it is being parsed by an entirely different parser with an entirely different grammar, with absolutely ZERO context the same. I'm all for making un-quoted unknown literals an E_ERROR instead of E_NOTICE if you can convince the Internals that the backwards-compatability break is good... But don't make me add a bunch of junk to the embedded variable inside quotes and heredoc! PHP already knows that these cannot be function names, nor contants, nor anything other than variables to be embedded there, because of the quotes or heredoc delimiters. -- Some people have a gift link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sloppy use of constants as strings. WAS: What does mean?
On 01/05/07, Daevid Vincent [EMAIL PROTECTED] wrote: echo EOF BROWSER: $_SERVER[HTTP_USER_AGENT] EOF; Isn't that form (sans quote marks) deprecated and frowned upon? ?php error_reporting( E_ALL ); echo EOF BROWSER: $_SERVER[HTTP_USER_AGENT] EOF; Why would cleaner, perfectly error free code be frowned upon? http://us2.php.net/manual/en/language.types.array.php A key may be either an integer or a string. If a key is the standard representation of an integer, it will be interpreted as such (i.e. 8 will be interpreted as 8, while 08 will be interpreted as 08). I was always under the impression that using: $_SERVER[HTTP_USER_AGENT] or $foo[myindex] Was bad compared to the proper way of: $_SERVER['HTTP_USER_AGENT'] or $foo['myindex'] True, but notice he's using it inside a heredoc string and the rules are slightly different for string parsing. http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing Whereas outside a string, $foo[myindex] will give you a notice and $foo['myindex'] is OK; Inside a string, $foo[myindex] will give you no notice and $foo['myindex'] will give you a parse error. The manual suggests that $foo[myindex] is treated the same inside a string as outside - if 'myindex' is defined as a constant then that is what's used as the index. This doesn't seem to be born out by reality. ?php error_reporting(E_ALL); $test = array( 'FOO' = 'Bareword', 'BAR' = 'Constant' ); define( 'FOO', 'BAR'); print \$test[FOO]: FOO is interpreted as a $test[FOO]\n; print {\$test[FOO]}: FOO is interpreted as a {$test[FOO]}\n; print {\$test['FOO']}: FOO is interpreted as a {$test['FOO']}\n; ? $ php5 test.php $test[FOO]: FOO is interpreted as a Bareword {$test[FOO]}: FOO is interpreted as a Constant {$test['FOO']}: FOO is interpreted as a Bareword -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] sloppy use of constants as strings. WAS: What does mean?
echo EOF BROWSER: $_SERVER[HTTP_USER_AGENT] EOF; Isn't that form (sans quote marks) deprecated and frowned upon? ?php error_reporting( E_ALL ); echo EOF BROWSER: $_SERVER[HTTP_USER_AGENT] EOF; Why would cleaner, perfectly error free code be frowned upon? http://us2.php.net/manual/en/language.types.array.php A key may be either an integer or a string. If a key is the standard representation of an integer, it will be interpreted as such (i.e. 8 will be interpreted as 8, while 08 will be interpreted as 08). I was always under the impression that using: $_SERVER[HTTP_USER_AGENT] or $foo[myindex] Was bad compared to the proper way of: $_SERVER['HTTP_USER_AGENT'] or $foo['myindex'] Just like it's: define('MYTHING', true); not define(MYTHING, true); (again, note the ' marks) http://us2.php.net/manual/en/function.define.php http://us2.php.net/manual/en/language.constants.php If you use an undefined constant, PHP assumes that you mean the name of the constant itself, just as if you called it as a string (CONSTANT vs CONSTANT). An error of level E_NOTICE will be issued when this happens. See also the manual entry on why $foo[bar] is wrong (unless you first define() bar as a constant). If you simply want to check if a constant is set, use the defined() function. So, I think you're relying upon a little work that PHP does for you in that an undefined CONSTANT is turned into a STRING i.e. bad. Personally, I *hate* that it does this work, and would love to see a little stricter parsing done and throw a fatal error if you try to use an undefined constant. D.Vin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] sloppy use of constants as strings. WAS: What does mean?
Personally, I *hate* that it does this work, and would love to see a little stricter parsing done and throw a fatal error if you try to use an undefined constant. You can do this yourself. See http://php.net/set_error_handler -- Postgresql php tutorials http://www.designmagick.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php