Notice: Undefined index: link

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
slewis
Forum Newbie
Posts: 3
Joined: Mon Oct 02, 2006 3:23 pm

Notice: Undefined index: link

Post 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
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:1. Select the correct board for your query. Take some time to read the guidelines in the sticky topic.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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'];
}
slewis
Forum Newbie
Posts: 3
Joined: Mon Oct 02, 2006 3:23 pm

More code

Post by slewis »

twigletmac | Please use

Code: Select all

,

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

,

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]
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

replace this:

Code: Select all

<?php $link = $_GET["link"];?>
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.
slewis
Forum Newbie
Posts: 3
Joined: Mon Oct 02, 2006 3:23 pm

Thank you

Post by slewis »

thank you
User avatar
SpecialK
Forum Commoner
Posts: 96
Joined: Mon Sep 18, 2006 3:49 pm

Post 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

Code: Select all

if(isset($_GET['link'])
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

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