php - Undefined index errors when using plus equal operator -


I am working on other developers old codes and there are many notifications: Unspecified index errors while using data + = operator The index is not necessarily set up so far, so that the error arises.

Example:

  $ myArray = array (); $ MyValue = 1; For {$ myArray ['test'] + = 1; ($ I = 1; $ i & lt; = 10; $ i ++) }   

An error will occur on the first run because the trial index is not set up yet.

I know that I can remove this error from the following code: $ myArray = array (); $ MyValue = 1; ($ I = 1; $ i & lt; = 10; $ i ++) {If ($ MyArray ['test'])) {$ myArray ['test'] = $ myValue; } And {$ myArray ['test'] + = $ myValue; }}

However, there are about 50 things to change in this way. Do all these issuing statements deserve to be written or do not I know better?

Edit: I should note that array indexes are not always equal and sometimes are not set, so I can not predict the pointer to avoid error in this situation.

It is a little small, but if you have a lot of edits, then maybe a bit complicated.

  $ MyArray = array (); $ MyValue = 1; {Isset ($ myArray ['test']) for ($ I = 1; $ i & lt; = 10; $ i ++)? $ MyArray ['test'] + = $ myValue: $ myArray ['test'] = $ myValue; }   

You can also type a global function (not tested).

  $ myArray = array (); $ MyValue = 1; {Increment ($ myArray ['test'], $ myValue for $ i = 1; $ i & lt; = 10; $ i ++); } Function increment (& $ var, $ inc) {$ var = isset ($ var)? $ Var + = $ inc: $ var = $ inc}    

Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -