Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-07-2013, 08:35 PM   PM User | #1
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,177
Thanks: 214
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
new define notices now and not before

hi,

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..
durangod is offline   Reply With Quote
Old 02-07-2013, 10:43 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
That would probably do it yeah.
PHP Code:
if (isset($_POST['avalue']))
{
    
$avalue $_POST['avalue'];

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.

Easiest way to save keystrokes it to use a ternary and give it a default:
PHP Code:
$avalue = isset($_POST['avalue']) ? $_POST['avalue'] : 'a default value.'
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
durangod (02-07-2013)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:04 PM.


Advertisement
Log in to turn off these ads.