Notice: Undefined offset solution, noticeundefined

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.