[thelist] "Notice: ..." errors
rich
rich at f1central.net
Tue Jul 13 11:26:51 CDT 2004
>
> > well they are warnings and IMO they indicate sloppy code or
> code expecting
> > register_globals to be on so I would personally steer clear of
> it as there
> > is likely to be other bugs or problems especially in a globals off
> > environment...
>
> This is not entirely true.
>
> The following code, which doesn't rely on register_globals will generate
> a Notice.
>
> <?php
> error_reporting(E_ALL);
> echo $_POST['x'];
> ?>
>
> However, slightly modified, it produces no Notices:
>
> <?php
> error_reporting(E_ALL);
> echo @$_POST['x'];
>
> //Another -- better way
>
> if ($_SERVER['REQUEST_METHOD'] == "POST")
> {
> echo $_POST['x'];
> }
> ?>
er... well I did use the word 'indicate' which (to me anyway) implies that I
am not stating an incontrovertible fact - I didn't say globals off code does
not generate Notice warnings -- it's just in my experience 'globals on' code
*tends* to generate Notices when running in a globals off environment...
however no matter what the environment I still think that 'finished' code
that generates Notices is basically sloppy code ...
also sorry to be pedantic but your 'better way' example code snippet above
is not a better way... take the example of a form where the posted data does
not contain the 'x' variable (eg an unchecked checkbox) and your code will
generate a ... 'Notice: Undefined index x in ...' error
IMO it would be better written as ...
<?php
if (isset($_POST['x'])) {
echo $_POST['x'];
}
?>
cheers
rich
More information about the thelist
mailing list