Notice: Undefined offset solution, noticeundefined
When debugging a program, there is always an error prompt:
Notice: Undefined offset: 1 in xxx. PhpOn line48
Notice: Undefined offset: 2 in xxx. PhpOn line48
Notice: Undefined offset: 3 in xxx. PhpOn line48
Notice: Undefined offset: 4 in xxx. PhpOn line48
This problem often occurs in the array, and the program can run correctly, but there will always be such a prompt on the screen: Notice: Undefined offset :..... The display of error_repoting in the php. ini file is usually restrained on the Internet. The parameter "EALL & Notice" is changed, so that the screen can be displayed normally.
The problem is solved, but it is hard to figure out the offset: Next number (for example, Notice: Undefined offset: 4 ....) What does it mean. also, the syntax in the sentence is clearly correct. Why is there a warning. after calmly thinking several times and trying every possibility, I finally found the answer. offset: the subsequent number is an incorrect array subscript, which is generally beyond the value range of the array. For example, an array $ A [] has 10 elements, if $ A [10] is displayed, an error occurs (Notice: Undefined offset: 10 ....), Because the subscript of the array starts from 0, the subscript of this array can only be 0 ~ 9. therefore, when such problems occur, do not rush to suppress the display (more easily add "error_reporting" at the beginning of the current file (fill in offset: the next number); Be sure to pay attention to the array subscript you are using. Think carefully and the problem will be quickly solved! It is also possible that the unset array is used to read its content. The php manual includes:
Just to confirm, using unset can destroy an entire array. I couldn't find reference to this anywhere so I decided to write this.
The difference between using unset and using $ myarray = array (); to unset is that obviusly the array will just be overwritten and will still exist.
<? Php
$ Myarray = array ("Hello", "World ");
Echo $ myarray [0]. $ myarray [1];
Unset ($ myarray );
// $ Myarray = array ();
Echo $ myarray [0]. $ myarray [1];
Echo $ myarray;
?>
Output with unset is:
<?
HelloWorld
Notice: Undefined offset: 0 in C: webpagesdainsidermyarray. php on line 10
Notice: Undefined offset: 1 in C: webpagesdainsidermyarray. php on line 10
Output with $ myarray = array (); is:
?>
<?
HelloWorld
Notice: Undefined offset: 0 in C: webpagesdainsidermyarray. php on line 10
Notice: Undefined offset: 1 in C: webpagesdainsidermyarray. php on line 10
Array
?>
This is the root cause of the problem.