Results 1 to 4 of 4

Thread: Odd WAMP error/notice with $_GET[]

  1. #1
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default Odd WAMP error/notice with $_GET[]

    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:

    PHP Code:
    $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?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. The Following User Says Thank You to djr33 For This Useful Post:

    jscheuer1 (03-07-2010)

  4. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Thanks Daniel. I had tried something like:

    PHP Code:
    $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:

    PHP Code:
    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •