SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Evangelist winterheat's Avatar
    Join Date
    Aug 2007
    Posts
    508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    is the call isset($_GET['flag']) really needed or can't we use if ($_GET['flag'])?

    frequently there is the use of

    if (isset($_GET['flag') && $_GET['flag') == 1) {
    # do something
    }


    or

    $num = isset($_GET['num']) ? $_GET['num'] : null;

    i just wonder if that is actually needed? coz we can just use

    if ($_GET['flag']) {
    # do something
    }


    or

    $num = $_GET['num'];

    directly too... is it that some PHP setting that will actually cause an error without using isset() because I see some user posting a note on

    http://www.php.net/isset
    mark dot fabrizio at gmail dot com
    15-Sep-2008 07:01

    that says we can use @$_REQUEST['test'] to avoid an error. thanks.

  2. #2
    SitePoint Guru
    Join Date
    Feb 2008
    Posts
    655
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by winterheat View Post
    $num = $_GET['num'];
    In this case if $_GET['num'] didn't exist, it would generate a warning (or is it a notice?)

  3. #3
    Use The Cloud
    Join Date
    Jan 2006
    Location
    Boise, ID
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using the @ operator will indeed suppress errors, but it's kind of like the difference between cleaning and shoving things under a rug.

  4. #4
    SitePoint Evangelist winterheat's Avatar
    Join Date
    Aug 2007
    Posts
    508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by corbyboy View Post
    In this case if $_GET['num'] didn't exist, it would generate a warning (or is it a notice?)
    oh really... i don't see any warning on the webpage... so maybe our PHP server actually has a setting to not show any warning huh? thanks.

  5. #5
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Not using isset() would generate a NOTICE which incidentally is hidden by default in most installations of PHP. However there is no excuse to not using isset() first.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  6. #6
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But to be in safe always, i do use isset() and in some cases !empty().
    PHP Code:
    if(isset($_GET['num'])){


    So that i don't get NOTICEs.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  7. #7
    SitePoint Evangelist winterheat's Avatar
    Join Date
    Aug 2007
    Posts
    508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    Not using isset() would generate a NOTICE which incidentally is hidden by default in most installations of PHP. However there is no excuse to not using isset() first.
    thanks for all your nice reply. so i wonder, if we can turn the Notice off, would it make it more concise and readable that if our code merely test for

    if ($_GET['flag'] == 2) { ... }

    instead of always checking the isset() first and then the value? thanks.

  8. #8
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Personally, I wouldn't check against $_GET (or any of the superglobal arrays that always exist) using isset()... I'd use
    Code:
    if (array_key_exists('key',$_GET))
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  9. #9
    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)
    Quote Originally Posted by Mark Baker View Post
    Personally, I wouldn't check against $_GET (or any of the superglobal arrays that always exist) using isset()... I'd use
    Code:
    if (array_key_exists('key',$_GET))
    Mark, that's really interesting, could you explain your preference in a little more detail?

    Obviously every programmer has their little nuances, but I'd love to hear your reasoning behind using this not so common method.
    Last edited by AnthonySterling; Nov 13, 2008 at 07:00. Reason: Poor grammar
    @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.

  10. #10
    SitePoint Evangelist winterheat's Avatar
    Join Date
    Aug 2007
    Posts
    508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    actually, come to think about it, probably Python actually error out and stop the program right there if a non-existent hash key is used.

    on the other hand, Ruby will just return nil instead.

    (Perl I think is like Ruby, just returning a null).

    and PHP will give a NULL but also print out a PHP notice. so it is kind of in the middle of Python and Ruby for how serious it treats a non-existent key.

  11. #11
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can factor the isset()'s out by merging with a array of default values first

    PHP Code:
        $defaults = array('num' => 0);
        
    $get array_merge($defaults$_GET);
        if (
    $get['num'] == 1)
        {
        }
        
    var_dump($get); 

  12. #12
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    Mark, that's really interesting, could you explain your preference in a little more detail?

    Obviously every programmer has their little nuances, but I'd love to hear your reasoning behind using this not so common method.
    I know array_key_exists() is a lot slower, and my rationale would not normally be appropriate to the superglobal array, but it's just a habit I've got into whenever working with any arrays.

    I use it mainly to handle null values:
    isset() will return a false if the variable is set to null: array_key_exists() will return true.
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint


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
  •