Undefined index: errors

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
jyhm
Forum Contributor
Posts: 228
Joined: Tue Dec 19, 2006 10:08 pm
Location: Connecticut, USA
Contact:

Undefined index: errors

Post by jyhm »

php spits out errors everytime a variable is not actually a part of url. I have 3 or 4 variables that when values are appeanded to the url will perform different functions. However the variables are not always appended in all circumstances so I write a conditional declaring the var if it is not already.

Code: Select all

$nba=$_GET ['nba'];// get variable from url

	if (!$nba)	{// check to see if $nba is set in the url
	 	define ("NBA", date("Ymd"));
		$nba_year=date("Y");
		$nba_month=$month;
		$nba_day=$day;
	 }	else {// if not set in the url make nba time today
	 	define("NBA", $nba);
		$nba_year=substr(NBA, 0, 4);// Careful! This converts numbers into strings!
		$nba_month=substr(NBA, 4, 2);// Careful! This converts numbers into strings!
		$nba_day=substr(NBA, 6, 2);// Careful! This converts numbers into strings!
	
	}
This actually works because of the else statement but before I get to the content I get an error message before it:

Notice: Undefined index: nba in /Users/jyhm/Sites/phpadv_ch1/nba_functions.php on line 20
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

it is common practice to check before it's existance, and if does not exist insert a default value. There are a couple ways to handle this, but the simplest is the use of an if() statement or ternary.

Code: Select all

$nba = !empty($_GET['nba']) ? $_GET['nba'] : 'default value';
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

It might be helpful to search these forums for 'undefined index' (using all terms). There are about 480 posts here that have covered this problem. :wink:
User avatar
jyhm
Forum Contributor
Posts: 228
Joined: Tue Dec 19, 2006 10:08 pm
Location: Connecticut, USA
Contact:

Post by jyhm »

Thank you jcart,
That really zapped all those php error messages. You must have read every php book available and the php.net/documentation line for line. Every time I find a solution, it is missing some sort of piece here or there that makes it inefficient. Do you recommend any reading material or references?

Sorry Everah I've never heard of '(using all terms)', I'll check to see what that is.
EDIT: ok sorry I see what you mean Everah, but honestly I don't think I saw a solution quit like what jcart presented. I would have thought an if isset or an if !isset would have accomplished the same result but it wasn't.

For those that wish to know the result.

Code: Select all

$nba = !empty($_GET['nba']) ? $_GET['nba'] : 'default value';
	if ($nba=='default value')	{// check to see if $nba is set in the url
	 	define ("NBA", date("Ymd"));
		$nba_year=date("Y");
		$nba_month=$month;
		$nba_day=$day;
	 }	elseif (isset($nba)) {// if not set in the url make nba time today
	 	define("NBA", $nba);
		$nba_year=substr(NBA, 0, 4);// Careful! This converts numbers into strings!
		$nba_month=substr(NBA, 4, 2);// Careful! This converts numbers into strings!
		$nba_day=substr(NBA, 6, 2);// Careful! This converts numbers into strings!
	
	}
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

jyhm wrote:I don't think I saw a solution quit like what jcart presented. I would have thought an if isset or an if !isset would have accomplished the same result but it wasn't.
Your original code did not make use of isset()...

Code: Select all

<?php
// If there is no URI parameter of 'nba' then $nba remains unset
$nba=$_GET ['nba'];// get variable from url

// If $nba is unset, this conditional will fail with undefined index errors
if (!$nba)      {// check to see if $nba is set in the url 
?>
What you could do is default $nba and change it only if the $_GET var is set

Code: Select all

<?php
$nba = 0;
if (isset($_GET['nba']))
{
  $nba = $_GET['nba'];
}
?>
Then you won't have that conditional setting, only a change if the conditional parameter is met.
User avatar
jyhm
Forum Contributor
Posts: 228
Joined: Tue Dec 19, 2006 10:08 pm
Location: Connecticut, USA
Contact:

Post by jyhm »

Ok, I think I understand. The code has a problem if its sittin' all by its lonely self;

Code: Select all

$nba=$_GET ['nba'];
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Everah wrote:

Code: Select all

<?php
$nba = 0;
if (isset($_GET['nba']))
{
  $nba = $_GET['nba'];
}
?>
Then you won't have that conditional setting, only a change if the conditional parameter is met.
You are literally doing the exact same thing as the ternary, but to what advantage?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

No else. It is cleaner to me. I went to this style almost a year ago seeing as new developers (or developers that are not as experienced as a Jcart or Everah) might not know what the ternary does. And since searching these forums is so hard for most :wink:, I didn't want to leave finding it up to anyone, so I use a longer version of what the ternary would do.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

jyhm wrote:Ok, I think I understand. The code has a problem if its sittin' all by its lonely self;

Code: Select all

$nba=$_GET ['nba'];
Right. What would $nba equal if $_GET['nba'] does not exist?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Everah wrote:No else. It is cleaner to me. I went to this style almost a year ago seeing as new developers (or developers that are not as experienced as a Jcart or Everah) might not know what the ternary does. And since searching these forums is so hard for most :wink:, I didn't want to leave finding it up to anyone, so I use a longer version of what the ternary would do.
Understood. I thought you were saying it was better. Indeed, it is a matter of personal preference. 8)
Z3RO21
Forum Contributor
Posts: 130
Joined: Thu Aug 17, 2006 8:59 am

Post by Z3RO21 »

I use array_key_exists, works like a charm for me :)

You can make use of objects and functions here. For example:

Code: Select all

function getVar($Var, $Default = FALSE) {
	if (array_key_exists($Var, $_GET) && !empty($_GET[$Var])) {
		return $_GET[$Var]; //If var exists, and is not empty return. You can omit `&& !empty($_GET[$Var])` if you return can be empty.
	} else {
		return $Default; //else return the dafault, the default of $Default is FALSE
	}
}
if (getVar('nba')) {
	echo '$GET[\'nba\'] exists!<br>';
}
$nba = getVar('nba', '$GET[\'nba\'] does not exist!');
echo $nba;
Results from http://127.0.0.1/ wrote: $_GET[nba] does not exist!
Results from http://127.0.0.1/?nba=123 wrote: $GET['nba'] exists!
123
Last edited by Z3RO21 on Tue Jan 02, 2007 3:39 pm, edited 1 time in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Z3RO21 wrote:I use array_key_exists, works like a charm for me :)
I use array_key_exists() myself. There are a thousand ways to skin a cat afterall..
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

array_key_exists() is nice (I use it myself when I need to know if an array key exists), but can be a slow alternative to isset() on larger arrays. For checking whether or not a superglobal is set (or anything for that matter) I always use isset().
Z3RO21
Forum Contributor
Posts: 130
Joined: Thu Aug 17, 2006 8:59 am

Post by Z3RO21 »

Everah wrote:array_key_exists() is nice (I use it myself when I need to know if an array key exists), but can be a slow alternative to isset() on larger arrays. For checking whether or not a superglobal is set (or anything for that matter) I always use isset().
Hmm never knew this. Thanks for the info Everah :)
Post Reply