SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard Darren884's Avatar
    Join Date
    Aug 2003
    Location
    Southern California, United States
    Posts
    1,616
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Help with configuring php and globals...

    How would I make it so I do not have to use the isset() function on a global like $_GET['variable'] to check whether it is set or not.

    if ($_GET['id'] == 1) {

    }

    will throw an error if $_GET['id'] is not set at all. How do I disregard this?
    Have a good day.

  2. #2
    SitePoint Zealot
    Join Date
    Dec 2005
    Posts
    117
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Simple, you don't.

    Code:
    if (isset($_GET['id']) && $_GET['id'] == 1) { 
    
    }

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Why would you NOT use the isset function?

    If there is a particular reason maybe you could explain and we could try to provide a solution, but to be fair, that's what isset is for.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    Pedantic Semantic blain's Avatar
    Join Date
    Mar 2006
    Location
    Yorkshire, UK
    Posts
    528
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    maybe use switch instead, then you can set a default

    PHP Code:

    switch($_GET['id']):
        default: 
    // do this if $_GET['id'] is not set
             
    break;
        case 
    1:
              break;
    endswitch; 
    Technology is dominated by two types of people:
    those who understand what they do not manage,
    and those who manage what they do not understand.

  5. #5
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Is it me, or is this same question being posed quite a lot lately?

    Have I missed something?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Depends what you are doing with it next.

    If its an integer you are going to use it a few times in your script and then do a db query with it, then typecast it and then test for not 0 (zero).

    $id = (int)$_GET['id'];

    if ( $id > 0 && $id === 1 ) {

    }
    // maybe later

    "select stuff from holdall where id = $id"

    If someone is able to send you a bad string of characters when you only expect an integer then typecast it to an integer, any failures are returned as 0.

    Though "123abc" is cast as 123.

    So OK, you are doing a second test, but instead of checking to see if something is set, you are in fact forcing it to be a certain value, and integer or 0, so its cleansed of any crack attempts too.

  8. #8
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can adjust php's error reporting level.

    Now, for starters, you should simply set display_errors to off when the website is in production mode where you get visitors. You only turn this setting on when developing. see ini_set() or change it in php.ini or .htaccess etc...

    If you don't want php to worry about E_NOTICE level errors(so that it won't record them in the error log either)
    PHP Code:
    error_reporting(E_ALL E_NOTICE); 
    http://www.php.net/error_reporting

  9. #9
    SitePoint Wizard Darren884's Avatar
    Join Date
    Aug 2003
    Location
    Southern California, United States
    Posts
    1,616
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi I turned notices off in php.ini and that worked. Thanx guys.
    Have a good day.


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
  •