Check if $_GET["menu"] is null.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nuno Paquete

    #1

    Check if $_GET["menu"] is null.

    Hi group.
    I'm using this code to see if is there any parameter for variable "menu":

    if($_GET["menu"] == "downloads" )
    ....

    But this code log errors if there is no parameter passed (this heappens at
    the first time the page is loaded).

    I tryed this code:

    if($_GET["menu"] != ""){
    if($_GET["menu"] == "downloads" )
    ...

    But this log the same error on the second line.
    Any help hould be greattly appreciated.
    Thanks in advance,

    Nuno Paquete
  • Gary L. Burnore

    #2
    Re: Check if $_GET["menu&quot ;] is null.

    On Fri, 30 Jul 2004 18:22:43 +0100, Nuno Paquete <nmp@ispgaya.pt >
    wrote:
    [color=blue]
    >Hi group.
    >I'm using this code to see if is there any parameter for variable "menu":
    >
    > if($_GET["menu"] == "downloads" )
    > ....
    >
    >But this code log errors if there is no parameter passed (this heappens at
    >the first time the page is loaded).
    >
    >I tryed this code:
    >
    > if($_GET["menu"] != ""){
    > if($_GET["menu"] == "downloads" )
    > ...[/color]

    Look at the isset() function.


    --
    gburnore@databa six dot com
    ---------------------------------------------------------------------------
    How you look depends on where you go.
    ---------------------------------------------------------------------------
    Gary L. Burnore | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
    | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
    DataBasix | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
    | ÝÛ³ 3 4 1 4 2 ݳ޳ 6 9 0 6 9 ÝÛ³
    Black Helicopter Repair Svcs Division | Official Proof of Purchase
    =============== =============== =============== =============== ===============
    Want one? GET one! http://signup.databasix.com
    =============== =============== =============== =============== ===============

    Comment

    • Jeffrey Silverman

      #3
      Re: Check if $_GET[&quot;menu&quot ;] is null.

      On Fri, 30 Jul 2004 18:22:43 +0100, Nuno Paquete wrote:
      [color=blue]
      > But this code log errors if there is no parameter passed (this heappens at
      > the first time the page is loaded).[/color]

      What errors? I don't see why you would get any. PHP is fairly quiet
      about using variables that are not yet set.

      --
      Jeffrey Silverman
      jeffreyPANTS@jh u.edu
      ** Drop "PANTS" to reply by email


      Comment

      • steve

        #4
        Re: Re: Check if $_GET[&quot;menu&quot ;] is null.

        "Gary L. Burnore" wrote:[color=blue]
        > On Fri, 30 Jul 2004 18:22:43 +0100, Nuno Paquete
        > <nmp@ispgaya.pt >
        > wrote:
        >[color=green]
        > >Hi group.
        > >I’m using this code to see if is there any parameter for[/color]
        > variable "menu":[color=green]
        > >
        > > if($_GET["menu"] == "downloads" )
        > > ....
        > >
        > >But this code log errors if there is no parameter passed (this[/color]
        > heappens at[color=green]
        > >the first time the page is loaded).
        > >
        > >I tryed this code:
        > >
        > > if($_GET["menu"] != ""){
        > > if($_GET["menu"] == "downloads" )
        > > ...[/color]
        >
        > Look at the isset() function.
        >
        >[/color]

        Try isset first followed by $_GET... as in
        if (isset(....) && $_GET[....] ) do something;

        php would not look at the $_GET in the above condition unless the
        left-side conditional isset(..) is True. By the way, even if you
        don’t use isset, you should get a warning, and not an error.

        --
        http://www.dbForumz.com/ This article was posted by author's request
        Articles individually checked for conformance to usenet standards
        Topic URL: http://www.dbForumz.com/PHP-Check-_G...ict135082.html
        Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=451195

        Comment

        • Garp

          #5
          Re: Re: Check if $_GET[&quot;menu&quot ;] is null.


          "steve" <UseLinkToEmail @dbForumz.com> wrote in message
          news:410aaaec$1 _4@news.athenan ews.com...[color=blue]
          > "Gary L. Burnore" wrote:[color=green]
          > > On Fri, 30 Jul 2004 18:22:43 +0100, Nuno Paquete
          > > <nmp@ispgaya.pt >
          > > wrote:
          > >[color=darkred]
          > > >Hi group.
          > > >I'm using this code to see if is there any parameter for[/color]
          > > variable "menu":[color=darkred]
          > > >
          > > > if($_GET["menu"] == "downloads" )
          > > > ....
          > > >
          > > >But this code log errors if there is no parameter passed (this[/color]
          > > heappens at[color=darkred]
          > > >the first time the page is loaded).
          > > >
          > > >I tryed this code:
          > > >
          > > > if($_GET["menu"] != ""){
          > > > if($_GET["menu"] == "downloads" )
          > > > ...[/color]
          > >
          > > Look at the isset() function.
          > >
          > >[/color]
          >
          > Try isset first followed by $_GET... as in
          > if (isset(....) && $_GET[....] ) do something;
          >
          > php would not look at the $_GET in the above condition unless the
          > left-side conditional isset(..) is True. By the way, even if you
          > don't use isset, you should get a warning, and not an error.
          >[/color]

          I'm partial to array_key_exist s($needle, $haystack) myself.

          Garp


          Comment

          • Tino Didriksen

            #6
            Re: Check if $_GET[&quot;menu&quot ;] is null.

            Like this:

            if (!empty($_GET['menu'])) {
            if ($_GET['menu'] == 'downloads') {
            ...
            } else if ...
            }

            -- Tino Didriksen / Project JJ

            Comment

            • Andy Hassall

              #7
              Re: Check if $_GET[&quot;menu&quot ;] is null.

              On Fri, 30 Jul 2004 21:07:49 GMT, "Garp" <i_am@home.no w> wrote:
              [color=blue][color=green][color=darkred]
              >>>>I'm using this code to see if is there any parameter for
              >>>> variable "menu":
              >>>>
              >>>> if($_GET["menu"] == "downloads" )
              >>>> ....
              >>>>
              >>>>But this code log errors if there is no parameter passed (this
              >>>>heappens at the first time the page is loaded).
              >>>
              >>> Look at the isset() function.[/color]
              >>
              >> Try isset first followed by $_GET... as in
              >> if (isset(....) && $_GET[....] ) do something;
              >>
              >> php would not look at the $_GET in the above condition unless the
              >> left-side conditional isset(..) is True. By the way, even if you
              >> don't use isset, you should get a warning, and not an error.[/color]
              >
              >I'm partial to array_key_exist s($needle, $haystack) myself.[/color]

              Whilst it seems a bit "naughty" from a purist point of view, I tend to use
              @$_GET['x'] - i.e. use error suppression, rather than "isset then use array
              key". So:

              if (@$_GET['x'] == "something" )
              // do something;
              else
              // either don't do something, or whinge that it's not an expected value

              I can't off the top of my head think of any other possible error that it could
              mask, other than the notice of use of undefined variable. In this case,
              undefined leads to the condition being false, so it doesn't get any further
              than that.

              --
              Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
              http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

              Comment

              • Zurab Davitiani

                #8
                Re: Check if $_GET[&quot;menu&quot ;] is null.

                Andy Hassall wrote:
                [color=blue]
                > Whilst it seems a bit "naughty" from a purist point of view, I tend to
                > use
                > @$_GET['x'] - i.e. use error suppression, rather than "isset then use
                > array key". So:
                >
                > if (@$_GET['x'] == "something" )
                > // do something;
                > else
                > // either don't do something, or whinge that it's not an expected value
                >
                > I can't off the top of my head think of any other possible error that it
                > could
                > mask, other than the notice of use of undefined variable. In this case,
                > undefined leads to the condition being false, so it doesn't get any
                > further than that.[/color]

                If you try these
                if(@$_GET['x'] == 0)
                if(@$_GET['x'] == false)
                then they will evaluate to true, even though x key is undefined. You can try
                with === but then you lose the flexibility of type conversion.

                Comment

                • Andy Hassall

                  #9
                  Re: Check if $_GET[&quot;menu&quot ;] is null.

                  On Fri, 30 Jul 2004 22:10:51 GMT, Zurab Davitiani <agt@mindless.c om> wrote:
                  [color=blue]
                  >Andy Hassall wrote:
                  >[color=green]
                  >> Whilst it seems a bit "naughty" from a purist point of view, I tend to
                  >> use
                  >> @$_GET['x'] - i.e. use error suppression, rather than "isset then use
                  >> array key". So:
                  >>
                  >> if (@$_GET['x'] == "something" )
                  >> // do something;
                  >> else
                  >> // either don't do something, or whinge that it's not an expected value
                  >>
                  >> I can't off the top of my head think of any other possible error that it
                  >> could
                  >> mask, other than the notice of use of undefined variable. In this case,
                  >> undefined leads to the condition being false, so it doesn't get any
                  >> further than that.[/color]
                  >
                  >If you try these
                  >if(@$_GET['x'] == 0)
                  >if(@$_GET['x'] == false)
                  >then they will evaluate to true, even though x key is undefined.[/color]

                  Yes, fair point, this falls over when expecting values that implicitly convert
                  to false. It's OK for doing switches e.g. on an 'action' value if you're only
                  expecting non-false values such as 'add', 'edit', etc., but it's dangerous
                  laziness if numeric values are coming in. Ah well, I knew it was bad really :-)

                  --
                  Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
                  http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

                  Comment

                  • Nuno Paquete

                    #10
                    Re: Check if $_GET[&quot;menu&quot ;] is null.

                    Andy Hassall wrote:
                    [color=blue]
                    > On Fri, 30 Jul 2004 21:07:49 GMT, "Garp" <i_am@home.no w> wrote:
                    >[color=green][color=darkred]
                    >>>>>I'm using this code to see if is there any parameter for
                    >>>>> variable "menu":
                    >>>>>
                    >>>>> if($_GET["menu"] == "downloads" )
                    >>>>> ....
                    >>>>>
                    >>>>>But this code log errors if there is no parameter passed (this
                    >>>>>heappens at the first time the page is loaded).
                    >>>>
                    >>>> Look at the isset() function.
                    >>>
                    >>> Try isset first followed by $_GET... as in
                    >>> if (isset(....) && $_GET[....] ) do something;
                    >>>
                    >>> php would not look at the $_GET in the above condition unless the
                    >>> left-side conditional isset(..) is True. By the way, even if you
                    >>> don't use isset, you should get a warning, and not an error.[/color]
                    >>
                    >>I'm partial to array_key_exist s($needle, $haystack) myself.[/color]
                    >
                    > Whilst it seems a bit "naughty" from a purist point of view, I tend to
                    > use
                    > @$_GET['x'] - i.e. use error suppression, rather than "isset then use
                    > array key". So:
                    >
                    > if (@$_GET['x'] == "something" )
                    > // do something;
                    > else
                    > // either don't do something, or whinge that it's not an expected value
                    >
                    > I can't off the top of my head think of any other possible error that it
                    > could
                    > mask, other than the notice of use of undefined variable. In this case,
                    > undefined leads to the condition being false, so it doesn't get any
                    > further than that.
                    >
                    > --
                    > Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
                    > http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space[/color]

                    Thank you guys. It's working without errors.
                    I used "@$_GET ..." but isset() function works perfectly.

                    Jeffrey, the error that I was getting is:

                    [Fri Jul 30 20:48:28 2004] [error] PHP Notice: Undefined index: menu in
                    home/nuno/public_html/header.php on line 124

                    Best regards,

                    Nuno Paquete

                    Comment

                    • Guest's Avatar

                      #11
                      Re: Check if $_GET[&quot;menu&quot ;] is null.

                      > What errors? I don't see why you would get any. PHP is fairly quiet[color=blue]
                      > about using variables that are not yet set.[/color]

                      // Reporting E_NOTICE can be good too (to report uninitialized
                      // variables or catch variable name misspellings ...)
                      error_reporting (E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
                      REF: http://us2.php.net/manual/en/functio...-reporting.php

                      _______________ _______________ ______________
                      Wil Moore III, MCP | Integrations Specialist


                      Comment

                      • Brad Kent

                        #12
                        Re: Check if $_GET[&quot;menu&quot ;] is null.

                        Yup, basically you want:

                        if( isset($_GET['menu']) && $_GET['menu'] == 'downloads' )

                        !empty() would also work. It goes the extra step to make sure it's
                        not '', 0, or an empty array..

                        also note a minor peeve of mine. I used single-quotes, not double
                        quotes!
                        single-quotes are not parsed and therefor more efficient.

                        Nuno Paquete <nmp@ispgaya.pt > wrote in message news:<410a83d5$ 0$30337$a729d34 7@news.telepac. pt>...[color=blue]
                        > Hi group.
                        > I'm using this code to see if is there any parameter for variable "menu":
                        >
                        > if($_GET["menu"] == "downloads" )
                        > ....
                        >
                        > But this code log errors if there is no parameter passed (this heappens at
                        > the first time the page is loaded).
                        >
                        > I tryed this code:
                        >
                        > if($_GET["menu"] != ""){
                        > if($_GET["menu"] == "downloads" )
                        > ...
                        >
                        > But this log the same error on the second line.
                        > Any help hould be greattly appreciated.
                        > Thanks in advance,
                        >
                        > Nuno Paquete[/color]

                        Comment

                        • Gary L. Burnore

                          #13
                          Re: Check if $_GET[&quot;menu&quot ;] is null.

                          On 30 Jul 2004 18:48:26 -0700, bkfake-google@yahoo.co m (Brad Kent)
                          wrote:
                          [color=blue]
                          >Yup, basically you want:
                          >
                          >if( isset($_GET['menu']) && $_GET['menu'] == 'downloads' )
                          >
                          >!empty() would also work. It goes the extra step to make sure it's
                          >not '', 0, or an empty array..[/color]

                          Learn something new every day.

                          [color=blue]
                          >
                          >also note a minor peeve of mine. I used single-quotes, not double
                          >quotes!
                          >single-quotes are not parsed and therefor more efficient.[/color]

                          Indeed. Of course, a pet peeve of mine is top posters.
                          [color=blue]
                          >
                          >Nuno Paquete <nmp@ispgaya.pt > wrote in message news:<410a83d5$ 0$30337$a729d34 7@news.telepac. pt>...[color=green]
                          >> Hi group.
                          >> I'm using this code to see if is there any parameter for variable "menu":
                          >>
                          >> if($_GET["menu"] == "downloads" )
                          >> ....
                          >>
                          >> But this code log errors if there is no parameter passed (this heappens at
                          >> the first time the page is loaded).
                          >>
                          >> I tryed this code:
                          >>
                          >> if($_GET["menu"] != ""){
                          >> if($_GET["menu"] == "downloads" )
                          >> ...
                          >>
                          >> But this log the same error on the second line.
                          >> Any help hould be greattly appreciated.
                          >> Thanks in advance,
                          >>
                          >> Nuno Paquete[/color][/color]

                          --
                          gburnore@databa six dot com
                          ---------------------------------------------------------------------------
                          How you look depends on where you go.
                          ---------------------------------------------------------------------------
                          Gary L. Burnore | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
                          | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
                          DataBasix | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
                          | ÝÛ³ 3 4 1 4 2 ݳ޳ 6 9 0 6 9 ÝÛ³
                          Black Helicopter Repair Svcs Division | Official Proof of Purchase
                          =============== =============== =============== =============== ===============
                          Want one? GET one! http://signup.databasix.com
                          =============== =============== =============== =============== ===============

                          Comment

                          • Chung Leong

                            #14
                            Re: Check if $_GET[&quot;menu&quot ;] is null.

                            "Zurab Davitiani" <agt@mindless.c om> wrote in message
                            news:LHzOc.1021 21$ve6.82922@ne wssvr29.news.pr odigy.com...[color=blue]
                            > Andy Hassall wrote:
                            >[color=green]
                            > > Whilst it seems a bit "naughty" from a purist point of view, I tend to
                            > > use
                            > > @$_GET['x'] - i.e. use error suppression, rather than "isset then use
                            > > array key". So:
                            > >
                            > > if (@$_GET['x'] == "something" )
                            > > // do something;
                            > > else
                            > > // either don't do something, or whinge that it's not an expected value
                            > >
                            > > I can't off the top of my head think of any other possible error that[/color][/color]
                            it[color=blue][color=green]
                            > > could
                            > > mask, other than the notice of use of undefined variable. In this case,
                            > > undefined leads to the condition being false, so it doesn't get any
                            > > further than that.[/color]
                            >
                            > If you try these
                            > if(@$_GET['x'] == 0)
                            > if(@$_GET['x'] == false)
                            > then they will evaluate to true, even though x key is undefined. You can[/color]
                            try[color=blue]
                            > with === but then you lose the flexibility of type conversion.[/color]

                            Well, those are pretty lousy examples, as they're both wrong. An empty
                            string will get cast to 0 on a comparison with a number, so if($_GET['x']==
                            0) will give you unexpected behavior even if do a isset() first. The correct
                            way to check if the user actually entered '0' is if($_GET['x'] == '0'),
                            which works as expected even if you do if(@$_GET['x'] == '0'). Likewise
                            if(@$_GET['x'] == false) is equally ambiguous, as GET variables are always
                            strings. All it does is check whether the user enter '0' or nothing.

                            Personally I see nothing wrong with ignoring the invalid index notices. In
                            practice there's little difference between index.php and
                            index.php?get=[empty]. These variable don't come out of nowhere after all.
                            If you have one script that expects variable X, then you probably have
                            another script that either post to that script or link to it.

                            The only case I can think of where there's a difference between a unset and
                            empty variable is a search page that submits to itself, where the former
                            means the user enters the page initially, while the latter means s/he didn't
                            enter a search criteria.


                            Comment

                            • Data Goob

                              #15
                              Re: Check if $_GET[&quot;menu&quot ;] is null.

                              Gary L. Burnore wrote:[color=blue]
                              >
                              > Indeed. Of course, a pet peeve of mine is top posters.
                              >[/color]
                              A pet peeve of mine are signatures with a lot of bullshit
                              and links to websites that don't exist.

                              http://signup.databasix.com doesn't work. In fact the domain
                              databasix.com doesn't exist. You might want to modify your
                              sig.



                              Comment

                              Working...