Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
02-07-2013, 08:35 PM #1
- Join Date
- Nov 2010
- Thanked 32 Times in 31 Posts
new define notices now and not before
i am not sure what it is but i have noticed lately that i have gotten alot of these
Notice: Undefined index: $value..............blah blah blah
This is something new and im not sure when this requirement began, i cleared all my notices on my last version of my script.
And now that i have gone to mysqli (which i cant see how that would make a difference here) i am starting to get these again. I am on php 5.3 right now and im not sure why every $value now it seems has to be defined ahead of time. I just never got so many before till now.
One in particular is just an error filter routine $value, when the page returns it runs thru a error value filter and displays error text on the screen.
UPDATE: i think i just figured it out, it think its because i was trying to save some keystrokes and i got rid of some of my isset usage.... i think thats why...
Last edited by durangod; 02-07-2013 at 11:15 PM.
02-07-2013, 10:43 PM #2
- Join Date
- Sep 2002
- Saskatoon, Saskatchewan
- Thanked 2,660 Times in 2,629 Posts
That would probably do it yeah.
for example would create $avalue out of $_POST['avalue'] so long as it exists. The downside is, if you don't contain this than $avalue is not available at all and any attempts to read it would trigger an undefined variable error.PHP Code:
$avalue = $_POST['avalue'];
Easiest way to save keystrokes it to use a ternary and give it a default:
Which guarantees $avalue will be set and will have a value regardless of if $_POST['avalue'] is set. Beyond this, you now operate on $avalue to perform validation and verification which may or may not fail your business rules, but will no longer throw an undefined offset nor undefined variable error.PHP Code:
$avalue = isset($_POST['avalue']) ? $_POST['avalue'] : 'a default value.';
header('HTTP/1.1 420 Enhance Your Calm');
Users who have thanked Fou-Lu for this post: