Page 1 of 1
Undefined index: errors
Posted: Tue Jan 02, 2007 1:09 pm
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
Posted: Tue Jan 02, 2007 1:11 pm
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';
Posted: Tue Jan 02, 2007 1:25 pm
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.

Posted: Tue Jan 02, 2007 1:40 pm
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!
}
Posted: Tue Jan 02, 2007 2:17 pm
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.
Posted: Tue Jan 02, 2007 2:24 pm
by jyhm
Ok, I think I understand. The code has a problem if its sittin' all by its lonely self;
Posted: Tue Jan 02, 2007 2:33 pm
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?
Posted: Tue Jan 02, 2007 2:48 pm
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

, I didn't want to leave finding it up to anyone, so I use a longer version of what the ternary would do.
Posted: Tue Jan 02, 2007 2:49 pm
by RobertGonzalez
jyhm wrote:Ok, I think I understand. The code has a problem if its sittin' all by its lonely self;
Right. What would $nba equal if $_GET['nba'] does not exist?
Posted: Tue Jan 02, 2007 3:24 pm
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

, 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.

Posted: Tue Jan 02, 2007 3:24 pm
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;
Posted: Tue Jan 02, 2007 3:25 pm
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..
Posted: Tue Jan 02, 2007 4:12 pm
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().
Posted: Tue Jan 02, 2007 8:36 pm
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
