Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help stop "undefined offset" warnings when setting arrays dynamically

Status
Not open for further replies.

southbeach

Programmer
Jan 22, 2008
879
US
I have arrays defined as
Code:
$titles = array();
$af		= array();
$am		= array();
$mn		= array();
$ot		= array();
$to		= array();

but when populating arrays using code such as
Code:
$am[$n][$y]++;

I get
Code:
Notice: Undefined offset: 3 in C:\wamp\[URL unfurl="true"]www\pos\veriscandailyreport.php[/URL] on line 173

This is mostly pointing to the $y value. The warning comes up once for each instance an element if set.

I could turn warning off and not see it, but I rather keep it ON on my development box so that I am aware of potential flaws.

What say you?

thank you all in advance for your assistance!


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Code:
If (!isset($am[$n][$y])) $am[$n][$y] = 0;
$am[$n][$y]++;
 
as jpadie pointed out you have an array slice that is null so when you try to increment nothing it dies. So either you have to initialize the value when you create it or check to see if the pointer is valid, initialize then increment.
 
or just turn notices off. there comes a time where the purity of writing code with all variables declared etc must take a back seat to getting a deliverable to a client on time. notices/warnings etc should, in any event, not be displayed in your production platforms (log them, by all means).
 
I have to disgree with this idea jpadie. You seem to be a passable coder. For all I know you might be the best php scripter on the planet. My following rant is aimed at this thought pattern that has emerged in todays programmers and not at you personally. Please don't take it as such.

Just because php isn't compiled doesn't mean you shouldn't follow best programming methodologies. Doing it half way may work but a internals patch could change that. Not likely but could. Warnings are there for a reason and should never be dismissed. Otherwise they would ALL be called notes.

This push and patch method of writing code to stabilize code you should have wrote correctly in the first place is absolutely foolish. Sure you make a quick buck but it inevitably promotes laziness. I'll fix this issue next patch kind of thing becomes the rule and not the exception. To the point that customers start shopping for a product that is more stable.

Sure the sales rep can sell ice to eskimos but in the end. We are the ones that make our companies the dollars. We are the ones who con the customer into loving our latest and greatest.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top