Page 1 of 1
Notice: Undefined index: link
Posted: Mon Oct 02, 2006 3:26 pm
by slewis
I am having an issue with my code, Notice: Undefined index: link.
this is what is throwing the Notice <?php $link = $_GET["link"];?>
How can I stop this notice.
Topic moved to PHP - Code
Posted: Mon Oct 02, 2006 3:30 pm
by Luke
that means that there was no array index "link" in the array $_GET. $_GET is a superglobal containing information sent within the uri of the request. if you attach ?link=foo to the end of your uri, that notice will go away... but that's not necessarily FIXING the problem, now is it? Maybe you should post a little more code?
generally, you want to check whether the array index is available before you attempt to use it... so so mething like this:
Code: Select all
// assign a default value to $link
$link = false;
if(array_key_exists('link', $_GET))
{
// If link was set in the uri, assign it to $link
$link = $_GET['link'];
}
More code
Posted: Mon Oct 02, 2006 3:35 pm
by slewis
twigletmac | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Code: Select all
<?php $link = $_GET["link"];?>
<div>
<?php
switch ($link) {
case "";
$content = "http://www.mprotary.com/home.php";
include $content;
break;
case "about":
$content = "http://www.mprotary.com/";
include $content;
break;
case "faq":
$content = "http://www.mprotary.com/faq.php";
include $content;
break;
}
?>
</div>
twigletmac | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Mon Oct 02, 2006 3:40 pm
by Luke
replace this:
with this:
Code: Select all
$link = false;
if(array_key_exists('link', $_GET))
{
// If link was set in the uri, assign it to $link
$link = $_GET['link'];
}
or if you want to get gangsta with it, use this...
Code: Select all
$link = array_key_exists('link', $_GET) ? $_GET['link'] : false;
fyi, the above "gangsta" version is using the
ternary operator.
Thank you
Posted: Mon Oct 02, 2006 3:49 pm
by slewis
thank you
Posted: Mon Oct 02, 2006 3:51 pm
by SpecialK
Ninja- I haven't used "array_key_exists()" before.
Is this considered more proper then using isset()?
Code: Select all
if(array_key_exists('link', $_GET))
or
Posted: Mon Oct 02, 2006 3:59 pm
by Luke
I suppose it's just a matter of preference. I haven't tested which is faster...
d11 said something about testing them, but I don't know if he ever did.