Very simple program logic problems. array judgment. for example, $ arr is an array. to determine whether it is null, you can use: if (! Empty ($ arr) {or if (count ($ arr) & gt; 0) {or if ($ arr !) {I often see this in other people's code, but I don't know what the difference is. why is it so useful? I hope you can talk about the experience. it is not very simple to use foreach to loop an array.
For example, $ arr is an array.
To determine whether it is null, you can use:
If (! Empty ($ arr )){
Or
If (count ($ arr)> 0 ){
Or
If ($ arr! = ''){
I often see this in other people's code, but I don't know what the difference is. why is it so useful?
Hope you can talk about your experience.
When we use foreach to loop an array, do we often need to make a non-empty judgment:
If (is_array ($ arr )&&! Empty ($ arr )){
------ Solution --------------------
Is_array ($ arr)
This is fine.
Null array does not report errors.
------ Solution --------------------
If you do not check the array before foreach, it is very dangerous to generate a warning error. If you use a string, you can use foreach to set a null value. if you use foreach to set a null value, the problem may occur.
There are many judgment methods, if (! Empty ($ arr) {} if (count ($ arr)> 0) {} is no big difference. Verify that the array is not empty...
------ Solution --------------------
When using foreach to loop an array, it is best to judge is_array
It is unnecessary to judge non-null values, and empty arrays do not enter the loop.
If you confirm that $ arr is an array
If (! Empty ($ arr )){
Or
If (count ($ arr)> 0 ){
And if ($ arr! = '') Not strict
------ Solution --------------------
Is_array () + count/empty is king.