$_GET and Querystring

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
oQEDo
Forum Commoner
Posts: 43
Joined: Thu Feb 20, 2003 11:08 am
Location: UK

$_GET and Querystring

Post by oQEDo »

I am passing a value in the querystring of a hyperlink and using the following code to trap the value:

$ItemID = $_GET["ID"];

if ($ItemID == "");
{
$ItemID = 1;
}

If no value is passed, $ItemID is set to 1, this works, however I get the notice:
Undefined index: ID

How do I prevent this?

Thanks in Adv.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

if no parameter ID is passed via GET that array $_GET does not have an element ID. That's what php is complaining about (not an error, only a warning)

Code: Select all

if(isset($_GET['ID']) && $_GET['ID'] !== '')
	$ItemID = (int)$_GET['ID'];
else
	$ItemID = 1;
User avatar
oQEDo
Forum Commoner
Posts: 43
Joined: Thu Feb 20, 2003 11:08 am
Location: UK

Post by oQEDo »

Thanks volka,
Still kinda new to php and that one had me stumped!

Is there a way to turn warnings off?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

http://www.php.net/manual/en/function.e ... orting.php but I strongly advise against decreasing the reporting-level while developing.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

I firmly agree with volka - lowering the error reporting doesn't fix the problem, it only hides it. Much better to learn how to prevent it in the first place:
http://www.php.net/manual/en/function.empty.php
http://www.php.net/manual/en/function.isset.php

Mac
User avatar
oQEDo
Forum Commoner
Posts: 43
Joined: Thu Feb 20, 2003 11:08 am
Location: UK

Tx

Post by oQEDo »

Thanks guys, i will not switch off the reporting, I am ashamed that I even thought about it :oops:
Post Reply