Page 1 of 1

$_GET and Querystring

Posted: Wed Apr 02, 2003 2:31 pm
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.

Posted: Wed Apr 02, 2003 3:26 pm
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;

Posted: Wed Apr 02, 2003 3:30 pm
by oQEDo
Thanks volka,
Still kinda new to php and that one had me stumped!

Is there a way to turn warnings off?

Posted: Wed Apr 02, 2003 5:19 pm
by volka
http://www.php.net/manual/en/function.e ... orting.php but I strongly advise against decreasing the reporting-level while developing.

Posted: Thu Apr 03, 2003 1:16 am
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

Tx

Posted: Thu Apr 03, 2003 7:50 am
by oQEDo
Thanks guys, i will not switch off the reporting, I am ashamed that I even thought about it :oops: