Log in

View Full Version : Odd WAMP error/notice with $_GET[]



jscheuer1
03-06-2010, 09:15 PM
I use WAMP for a PHP 'sandbox' and recently got this error/notice (path altered, but unimportant):


Notice: Undefined index: status in wamp\. . . \tickets\index.php on line 58

Line 58 looks like so:


$theStatus = $_GET["status"];

It only occurs if there is no (or similar query string):


?status=whatever

at the end of the address in the address bar for the page. And it doesn't white out the page, it only displays at the location in the code where the PHP code is, the rest of the page displays fine.

When the page is live, there is no error/notice regardless of the query string or lack thereof.

Anyone know what this is about? Could I turn it off in WAMP? If so, should I?

djr33
03-06-2010, 11:32 PM
This is not an error but a notice. It's letting you know that the "status" index of that array is not defined so you should not be referring to it unless it is defined. You can suppress this error/notice like so:
$status = @$_GET['status'];

Or, the more correct way to do this is:
$status = '';
if (isset($_GET['status'])) {
$status = $_GET['status'];
}

Shorthand:
$status = isset($_GET['status'])?$_GET['status']:'';


This is a constant annoyance for PHP because technically you should not be calling undefined indexes, and of course it's helpful in all cases except get/post variables.

Many programmers are patient enough to write out all that code each time, and some aren't.



The reason this differs on the two servers is because of the error reporting level.
Lots of info here
http://php.net/manual/en/errorfunc.configuration.php
Or just google "error reporting" for more information than anyone really wants.
You can set the error reporting level in at least 4 ways:
php.ini (this is where it differs for you).
.htaccess (using php_ini_set, I think)
at runtime using: ini_set('.....')
at runtime using: error_reporting()

They're listed in the order of most to least powerful, and also (I believe) first to last processed so one will override the others in that order.

Lazier programmers will just turn down the error reporting level so that they aren't told about this and it will never be a problem, but you also won't know when there is an undefined index so you'll possibly get unexpected results.


The best way is to check if it's set then proceed. Do that if you wish to spend the extra time.

jscheuer1
03-07-2010, 12:25 AM
Thanks Daniel. I had tried something like:



$status = '';
if (isset($_GET['status'])) {
$status = $_GET['status'];
}

But I hadn't gotten the syntax right. I didn't realize I needed to use the $_GET[] within the isset(). I was doing:


if(isset("status"))

That just made things worse. Writing extra code isn't a big deal for me, especially as you say that there is a shorthand for it. And since, as you also seem to say, this notice is important for other types of code, I probably shouldn't turn it off in WAMP.

djr33
03-07-2010, 12:34 AM
Correct.

The last consideration is that for a beta-tested final site, you may want to turn off ALL error reporting to prevent anyone from possibly hacking the server based on an unexpected error. It's unlikely, but in the event of a site breakdown doesn't reveal anything to your users. You can, if things stop working, of course turn it back on.
Also, if there are casual errors (fatal errors will stop processing the page), then they won't be shown and the site can continue like normal without interrupting your visitor's experience with an error in the middle of the layout.