473,698 Members | 2,426 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 17 '05
32 33028
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
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
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
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

"Brian" <us*****@juliet remblay.com.inv alid> wrote in message
news:10******** *****@corp.supe rnews.com...
I must admit, I'm confused. I've been using the following code:

if ($_POST['referringpage'] <> 'catalogue') {
header("Locatio n: 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
"Zurab Davitiani" <ag*@mindless.c om> wrote in message
news:Hh******** *********@newss vr21.news.prodi gy.com...
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
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

"Zurab Davitiani" <ag*@mindless.c om> wrote in message
news:L4******** ************@ne wssvr29.news.pr odigy.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
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
In article <GJ************ ********@newssv r29.news.prodig y.com>,
Zurab Davitiani <ag*@mindless.c om> 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
45636
by: Luis | last post by:
Is there an easier way to check if any of the fields returned in a select statement have null values? After running this command: set rs = conn.execute("select A,B,C,D,E,F,G,H,I,J from SomeTable where...") I could check each field this way:
2
13883
by: sam | last post by:
For Example, If I delete two times like the below code, the program will behave differently. is this correct? As C++ FAQ the delete checks where or not f1 has memory. How can the following program can go wrong? class foo{ // }; int main{ foo *f1 = new foo()
2
3956
by: ad | last post by:
I have a string like string sNo. I want to cast it to int with int.Parse(sNo) but if the string is null or empty, it will thow exception. I must check if sNo is not null and is not empty before cast like: if ((sNo<>null) && (sNo<>"")) Have there any function to check the null and "" at once
1
4279
by: MattH | last post by:
I can't seem to figure out the syntax for checking for null values in asp.net. Can anyone point me in the right direction? Also, is there a common area on the MSDN for syntax lookup? Thanks!
1
1267
by: WebBuilder451 | last post by:
Why will this not work? it comes back with an error saying it can't work because the value is null this is a dataset value retreaved from db and it is null. call to check for null Me.txtExtra1.Text = Me.MNS(.usrExtra1) function to return a default value: Public Function MNS(ByVal s As Object) As String If IsDBNull(s) Then Return String.Empty
15
128300
by: Tarun Mistry | last post by:
Hi guys, what is the best/correct way to check for a NULL object? I.e. myClass test; if(test == null) {}
3
10692
by: Jim | last post by:
I am trying to use a combo box in order to write a query on the fly. I want to check for a null entry so i can replace it with "Like *" or similar. If Combo0.Value = Null Then MsgBox ("null") this however doesnt seem to work... even when there is no value set the msgbox doesnt appears any help much appreciated...
4
4640
by: Richard Coltrane | last post by:
Hi there, Im stepping into C# from VB.net. In all the examples ive seen about raising events the following construct is used: if (myevent != null) myevent(this,args); Whats the purpose of the test for null? Is that testing to see if the underlying delegate is null? If so when would it be?
1
1811
by: Sparky74 | last post by:
Hi Everybody. I have been searching for many hours for an answer to this problem. I hope somebody can help me. I have a C# .NET client application that connects to a TCP/IP C++ server application that I have maintained for a number of years. What I am finding is that Socket.Close() does not actually sever the client connection from my server application. Actually, my statement above isn't entirely true - Socket.Close() *does* work as...
2
14655
by: qwedster | last post by:
Folk! How to programattically check if null value exists in database table (using stored procedure)? I know it's possble in the Query Analyzer (see last SQL query batch statements)? But how can I pass null value as parameter to the database stored procedure programattically using C#? Although I can check for empty column (the following code passes string.Empty as parameter but how to pass null value?), I cannot check for null value...
0
8678
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9166
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6525
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4621
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.