PHP is very efficient in development, this is understandable, but it is at the expense of implementation efficiency. PHP Array function is very powerful, but also need to consider more, try several situations, just in case, here, I simply said that two encountered the pit, later if there are more to find, and then fill it!
foreach provides a simple way to traverse an array to easily read the contents of data or objects, but the official document says that because foreach relies on an internal array pointer, modifying its value in a loop can result in 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, the use of ' & ' is a safe way, although rarely used, however, when used, the unset function should be called immediately after the reference is destroyed, otherwise the last value of the loop will be modified to get an unexpected value if it is used again in the next code. For example, when the list loops out, the last line of output will appear with a lot of messy values or null values. Use unset to resolve this issue.
Let's take a look at an example
<?php/*-------------------------------------------------------------------------* *
foreach Example 1:value only */
echo "foreach example 1:value only". <br/> ';
$a = Array (1, 2, 3,);
foreach ($a as $v) {
echo "current value of". $a. ":". $v. " <br/> ";
}
? >
//Run result
foreach example 1:value only
value of $a: 1 current
value of $a: 2
current Value of $a: 3
In_array, which means checking whether the previous string exists in the next array, and in most cases it works, but when the following array is an integer, such as Array (0,1,2,3), the problem occurs. PHP intval The previous string so that it gets a value of 0, so if you happen to have this value in your array, then the equation is set up, is it going beyond expectations?
So, when you're sure that the next data is an integer, especially if it's probably 0 (this might replace all the strings), you can never use the function again, but use key_exists instead, but the following data will need to be inverted using array_flip.
Let's take a look at another example.
function Search ($keyWord, $stack) {//This determines whether to update or insert a
foreach ($stack as $key => $val) {
if In_array ($keyWord, $ val)) {return
TRUE;
}
}
return FALSE;
}
What happens when a string is used as an array to fetch values? PHP is a highly fault tolerant language, it will try to help you correct the error, so it is smart to convert your reference to an integer, of course, you get 0, then the string subscript 0 values will get the value of the first string, is not beyond your expectations? The solution is to confirm that the variable is not an array before referencing the subscript, Is_array.
Although many of the predecessors encountered such or such a problem, but also kept saying, but we are unavoidable, and constantly to make mistakes. This may be the Society bar! Everyone is so busy, there is no time to crack your rotten code! Ha ha