Some of the less common questions in PHP foreach/in_array,foreachin_array_php tutorial

Source: Internet
Author: User

Some of the less common problems in PHP Foreach/in_array,foreachin_array


PHP is very efficient in development, which is understandable, but at the expense of execution efficiency. PHP Array function is very powerful, but also to consider more, try a few situations, just in case, here, I simply say two encounters of the pit, later if there are found more, then fill it!

  foreach provides a simple way to iterate through an array, and it is easy to read the contents of the data or object, but the official document says that because foreach relies on an internal array pointer, modifying its value in a loop can lead to unexpected behavior. So, basically, 1, do not want to change inside the loop within the value, otherwise the result will be more than you want, 2, using ' & ' is a safe way, although rarely used, but when used, at the end of the reference should immediately call the unset function to destroy the variable, otherwise, in the next code, If this variable is used again, the last value of the loop is modified, resulting in unexpected values, such as: When the list is looped out, the last line of output will have many messy values or null values. Use unset to resolve this issue.

In_array, the meaning is to check whether the previous string exists in the latter array, and in most cases, it works, but when the following array is an integer, such as Array (0,1,2,3), there is a problem, PHP will be the former string intval, So you get a value of 0, so if you happen to have this value in your array, then the equation is set up, is it more than expected? So, when you're sure that the data that follows is an integer, especially if it's probably 0 (which might replace all of the strings), you can't use this function anymore, but you can use key_exists instead, but the data behind it needs to be reversed using Array_flip.

What happens when a string is used as an array to fetch values? PHP is a very fault-tolerant language, it will try to help you correct the error, so it is very clever to convert your reference subscript into an integer, of course, you get 0, then the string subscript 0 to get the value of the first string, is it more than you expected? The workaround is to confirm that the variable is not an array before referencing the subscript, Is_array.

Although there are many of the predecessors encountered such or such problems, but also kept saying, but we are inevitably unavoidable, and continue to make mistakes. This may be the society! Everyone is so busy, there is no time to crack your bad code! Ha ha

http://www.bkjia.com/PHPjc/1066745.html www.bkjia.com true http://www.bkjia.com/PHPjc/1066745.html techarticle some of the less common problems in PHP foreach/in_array,foreachin_array PHP in the development of high efficiency, this is understandable, but at the expense of execution efficiency. PHP array features very strong ...

  • 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.