By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,302 Members | 1,812 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,302 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
32 Replies


P: n/a
On Fri, 30 Jul 2004 18:22:43 +0100, Nuno Paquete <nm*@ispgaya.pt>
wrote:
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")
...


Look at the isset() function.
--
gburnore@databasix 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
================================================== =========================
Jul 17 '05 #2

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


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
je**********@jhu.edu
** Drop "PANTS" to reply by email
Jul 17 '05 #3

P: n/a
"Gary L. Burnore" wrote:
On Fri, 30 Jul 2004 18:22:43 +0100, Nuno Paquete
<nm*@ispgaya.pt>
wrote:
Hi group.
Im 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")
...


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
dont 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
Jul 17 '05 #4

P: n/a

"steve" <Us************@dbForumz.com> wrote in message
news:41**********@news.athenanews.com...
"Gary L. Burnore" wrote:
> On Fri, 30 Jul 2004 18:22:43 +0100, Nuno Paquete
> <nm*@ispgaya.pt>
> wrote:
>
> >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")
> > ...

>
> 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.


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

Garp
Jul 17 '05 #5

P: n/a
Like this:

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

-- Tino Didriksen / Project JJ
Jul 17 '05 #6

P: n/a
On Fri, 30 Jul 2004 21:07:49 GMT, "Garp" <i_**@home.now> wrote:
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.


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


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 <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #7

P: n/a
Andy Hassall wrote:
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.


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.
Jul 17 '05 #8

P: n/a
On Fri, 30 Jul 2004 22:10:51 GMT, Zurab Davitiani <ag*@mindless.com> wrote:
Andy Hassall wrote:
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.


If you try these
if(@$_GET['x'] == 0)
if(@$_GET['x'] == false)
then they will evaluate to true, even though x key is undefined.


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 <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #9

P: n/a
Andy Hassall wrote:
On Fri, 30 Jul 2004 21:07:49 GMT, "Garp" <i_**@home.now> wrote:
>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.


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


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 <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space


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

Jul 17 '05 #10

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


// 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
Jul 17 '05 #11

P: n/a
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 <nm*@ispgaya.pt> wrote in message news:<41***********************@news.telepac.pt>.. .
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

Jul 17 '05 #12

P: n/a
On 30 Jul 2004 18:48:26 -0700, bk***********@yahoo.com (Brad Kent)
wrote:
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..
Learn something new every day.


also note a minor peeve of mine. I used single-quotes, not double
quotes!
single-quotes are not parsed and therefor more efficient.
Indeed. Of course, a pet peeve of mine is top posters.

Nuno Paquete <nm*@ispgaya.pt> wrote in message news:<41***********************@news.telepac.pt>.. .
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


--
gburnore@databasix 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
================================================== =========================
Jul 17 '05 #13

P: n/a
"Zurab Davitiani" <ag*@mindless.com> wrote in message
news:LH********************@newssvr29.news.prodigy .com...
Andy Hassall wrote:
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.
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.


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.
Jul 17 '05 #14

P: n/a
Gary L. Burnore wrote:

Indeed. Of course, a pet peeve of mine is top posters.

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.

Jul 17 '05 #15

P: n/a
"Nuno Paquete" <nm*@ispgaya.pt> wrote in message
news:41***********************@news.telepac.pt...
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


No! Not another lame-o single-entry point setup!

How about <a href="downloads.php">Download</a>?
Jul 17 '05 #16

P: n/a
"Data Goob" <da******@hotmail.com> wrote in message
news:os*******************@fe28.usenetserver.com.. .
Gary L. Burnore wrote:

Indeed. Of course, a pet peeve of mine is top posters.

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.


Modify it to 4 lines or less, while you're at it.

Garp
Jul 17 '05 #17

P: n/a
On Fri, 30 Jul 2004 17:12:27 -0700, laidbak69 wrote:
> // 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);


Sounds like -w or "use strict;" in Perl. Yes?

--
Jeffrey Silverman
je**********@jhu.edu
** Drop "PANTS" to reply by email
Jul 17 '05 #18

P: n/a
On Sat, 31 Jul 2004 09:13:49 -0400, Jeffrey Silverman <je**********@jhu.edu>
wrote:
On Fri, 30 Jul 2004 17:12:27 -0700, laidbak69 wrote:
>> // 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);


Sounds like -w or "use strict;" in Perl. Yes?


It's not really like "use strict;" as you can't explicitly declare variables
in PHP anyway, although it is like one part of "use warnings;" (the modern
version of the -w flag), in particular "use warnings 'uninitialized';".

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #19

P: n/a
steve wrote:
Nuno Paquete wrote:
this code log errors if there is no parameter passed

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


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
dont use isset, you should get a warning, and not an error.


I must admit, I'm confused. I've been using the following code:

if ($_POST['referringpage'] <> 'catalogue') {
header("Location: etc."

If there's no post variable 'referringpage', I don't get any warning.
How come? (I'm not complaining, just anxious to avoid future problems.)

--
Brian (remove ".invalid" to email me)
http://www.tsmchughs.com/
Jul 17 '05 #20

P: n/a
Chung Leong wrote:
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').
I don't think it's *the* correct way. Call it "lousy examples" all you want.
What if you are comparing the $_GET['x'] to an integer variable that's set
to 0 and not to string '0'?
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.


I didn't give any practical examples. Yes, it's not likely to happen but
there is a remote chance that a notice during development may slip by. And
a remote chance after that it will have an unintended effect on your
script. So, I don't think it's "clean" way to do it.
Jul 17 '05 #21

P: n/a
On Sat, 31 Jul 2004 19:20:07 +0000, Zurab Davitiani wrote:
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').


I don't think it's *the* correct way. Call it "lousy examples" all you want.
What if you are comparing the $_GET['x'] to an integer variable that's set
to 0 and not to string '0'?

if ($_GET['x'] === 0) {

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #22

P: n/a
Ian.H wrote:
I don't think it's *the* correct way. Call it "lousy examples" all you
want. What if you are comparing the $_GET['x'] to an integer variable
that's set to 0 and not to string '0'?

if ($_GET['x'] === 0) {


And that will evaluate to false because $_GET['x'] will be a string. I
alluded to that option in my previous post in this thread and pointed out
you would lose the implicit type convertion capability in PHP; at least in
PHP4 - I believe there were some type convertion changes in PHP5 that I
haven't completely tried out yet, but that's a different issue.
Jul 17 '05 #23

P: n/a
On Sun, 01 Aug 2004 01:28:39 +0000, Zurab Davitiani wrote:
Ian.H wrote:
I don't think it's *the* correct way. Call it "lousy examples" all you
want. What if you are comparing the $_GET['x'] to an integer variable
that's set to 0 and not to string '0'?

if ($_GET['x'] === 0) {


And that will evaluate to false because $_GET['x'] will be a string. I
alluded to that option in my previous post in this thread and pointed out
you would lose the implicit type convertion capability in PHP; at least in
PHP4 - I believe there were some type convertion changes in PHP5 that I
haven't completely tried out yet, but that's a different issue.

Apologies Zurab, I didn't see your previous post regarding this issue and
to make it worse, I misread / misunderstood your point above initially.. I
now see that you weren't infacting asking _how_ to do the comparison of an
INT =)

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/

Jul 17 '05 #24

P: n/a

"Brian" <us*****@julietremblay.com.invalid> wrote in message
news:10*************@corp.supernews.com...
I must admit, I'm confused. I've been using the following code:

if ($_POST['referringpage'] <> 'catalogue') {
header("Location: etc."

If there's no post variable 'referringpage', I don't get any warning.
How come? (I'm not complaining, just anxious to avoid future problems.)


Try putting error_reporting(E_ALL) first and see what it does then, then
check your default error reporting level in your PHP.INI?

Garp
Jul 17 '05 #25

P: n/a
"Zurab Davitiani" <ag*@mindless.com> wrote in message
news:Hh*****************@newssvr21.news.prodigy.co m...
I don't think it's *the* correct way. Call it "lousy examples" all you want. What if you are comparing the $_GET['x'] to an integer variable that's set
to 0 and not to string '0'?


Well, you can always use strcmp() or what not, but the bottomline is that
it's a string comparison. If the comparison is to a variable then you cast
the value into a string, that's all.

The point here is that using isset() does not automatically resolve
potential ambiguity. If it matters whether the user enter an empty string or
'0', then check for it explicitly with strlen(@$_GET['x']) > 0. If it
matters whether the variable is present, then use isset().
--
Obey the Clown - http://www.conradish.net/bobo/

Jul 17 '05 #26

P: n/a
Chung Leong wrote:
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.

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.
Now I see you missed one detail. And I, in turn, missed that in my previous
reply to this post. You are assuming here the $_GET['x'] is defined (to an
empty string or 0), while I gave my examples for when x key is undefined.
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.


Yes, you give a specific example of what I was saying to begin with. So, in
more general sense, there could be a case of ?option=0 where you could have
an option-specific logic; and a default (when option is not specified):

if(@$_GET['option'] == 0)
//do option 0
else
//do default

In this case, if option is undefined, the else block will not execute
because the first condition is true; and you wouldn't get a PHP notice
either that option is not set.

So, I think we are saying the same thing.
Jul 17 '05 #27

P: n/a

"Zurab Davitiani" <ag*@mindless.com> wrote in message
news:L4********************@newssvr29.news.prodigy .com...
Yes, you give a specific example of what I was saying to begin with. So, in more general sense, there could be a case of ?option=0 where you could have an option-specific logic; and a default (when option is not specified):

if(@$_GET['option'] == 0)
//do option 0
else
//do default

In this case, if option is undefined, the else block will not execute
because the first condition is true; and you wouldn't get a PHP notice
either that option is not set.

So, I think we are saying the same thing.


The same thing would happen if you do if(isset($_GET['option']) &&
$_GET['option'] == 0) and $_GET['option'] is empty. The point is that
isset() doesn't help here. What you need to do is if($_GET['option'] ==
'0')--which I call "the correct way" and which you refuse to believe,
Jul 17 '05 #28

P: n/a
Chung Leong wrote:
if(@$_GET['option'] == 0)
//do option 0
else
//do default

In this case, if option is undefined, the else block will not execute
because the first condition is true; and you wouldn't get a PHP notice
either that option is not set.

So, I think we are saying the same thing.
The same thing would happen if you do if(isset($_GET['option']) &&
$_GET['option'] == 0) and $_GET['option'] is empty. The point is that
isset() doesn't help here.


Actually, isset helps because in the example that I gave, $_GET['option'] is
*undefined* (not "empty" but only *undefined*) and the isset call will
evaluate to false. Without isset, the condition in my example will evaluate
to true.
What you need to do is if($_GET['option'] ==
'0')--which I call "the correct way" and which you refuse to believe,


Because once you determine that $_GET['option'] is *defined* then both of
these:

$_GET['option'] == '0'
$_GET['option'] == 0

will do exactly the same.

And because I believe in PHP's ability and flexibility of implicit type
conversion. Say you have a variable $x:

$x = 0; //say you got this value from a database numeric field

//now you have
if(@$_GET['option'] == $x)
// do stuff for option 0
else
// do default stuff

Now, again, if $_GET['option'] is *undefined* and you want to go to default
block in that case, you cannot use this logic; because if $_GET['option']
is *undefined* the first if condition will evaluate to true. You would have
to either:

1. use isset to determine whether $_GET['option'] defined or not first; OR,
2. like you said, convert $x explicitly to a string yourself or use a string
comparison function.

I wouldn't say the (2) is "the correct way" and the (1) is a "wrong way." In
fact, I prefer (1). But you may obviously disagree.
Jul 17 '05 #29

P: n/a
In article <GJ********************@newssvr29.news.prodigy.com >,
Zurab Davitiani <ag*@mindless.com> wrote:
Chung Leong wrote:
if(@$_GET['option'] == 0)
//do option 0
else
//do default

In this case, if option is undefined, the else block will not execute
because the first condition is true; and you wouldn't get a PHP notice
either that option is not set.

So, I think we are saying the same thing.


The same thing would happen if you do if(isset($_GET['option']) &&
$_GET['option'] == 0) and $_GET['option'] is empty. The point is that
isset() doesn't help here.


Actually, isset helps because in the example that I gave, $_GET['option'] is
*undefined* (not "empty" but only *undefined*) and the isset call will
evaluate to false. Without isset, the condition in my example will evaluate
to true.
What you need to do is if($_GET['option'] ==
'0')--which I call "the correct way" and which you refuse to believe,


Because once you determine that $_GET['option'] is *defined* then both of
these:

$_GET['option'] == '0'
$_GET['option'] == 0

will do exactly the same.

And because I believe in PHP's ability and flexibility of implicit type
conversion. Say you have a variable $x:

$x = 0; //say you got this value from a database numeric field

//now you have
if(@$_GET['option'] == $x)
// do stuff for option 0
else
// do default stuff

Now, again, if $_GET['option'] is *undefined* and you want to go to default
block in that case, you cannot use this logic; because if $_GET['option']
is *undefined* the first if condition will evaluate to true.


If option is undefined, then I would have thought that $_GET['option']
would also be undefined. If its undefined it cannot be equal to zero or
any other value, so why is the else clause not taken?

-- tim
Jul 17 '05 #30

P: n/a
Zurab Davitiani wrote:
Because once you determine that $_GET['option'] is *defined* then both of
these:

$_GET['option'] == '0'
$_GET['option'] == 0

will do exactly the same.


I'm replying to myself here, but I believe I made a mistake. These 2 are not
exactly the same since you'd still need to do an empty check in the second
case. Or strcomp.

Anyway, if you don't need to know about whether a GET variable is defined or
not, you don't need to use ==, you can just use strcomp. I think that's
what Chung Leong said, and I don't really disagree there.
Jul 17 '05 #31

P: n/a
Tim Streater wrote:
If option is undefined, then I would have thought that $_GET['option']
would also be undefined. If its undefined it cannot be equal to zero or
any other value, so why is the else clause not taken?


That's a big part of what this sub-thread was about. Because undefined value
is equal to 0/false/[empty string] with implicit type conversion. If you
need to know whether a variable is defined or not, use isset; and don't
turn off undefined notices to catch them. If it doesn't matter and you are
just comparing string values, use strcomp (or another way of string-only
comparison).
Jul 17 '05 #32

P: n/a
"Zurab Davitiani" <ag*@mindless.com> wrote in message
news:0n****************@newssvr27.news.prodigy.com ...
Zurab Davitiani wrote:
Because once you determine that $_GET['option'] is *defined* then both of these:

$_GET['option'] == '0'
$_GET['option'] == 0

will do exactly the same.
I'm replying to myself here, but I believe I made a mistake. These 2 are

not exactly the same since you'd still need to do an empty check in the second
case. Or strcomp.

Anyway, if you don't need to know about whether a GET variable is defined or not, you don't need to use ==, you can just use strcomp. I think that's
what Chung Leong said, and I don't really disagree there.


I made somewhat of an mistake here too. Forgot that *any* non-numeric
string gets converted to 0 on numeric comparison. So the following is true:

if("Bobo the Clown" == 0) {
echo "Bobo is nothing!";
}

But the bottomline is that isset() doesn't help you here.

--
Obey the Clown - http://www.conradish.net/bobo/
Jul 17 '05 #33

This discussion thread is closed

Replies have been disabled for this discussion.