Simple program logic problems. array-based judgment
Source: Internet
Author: User
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? Hope you can talk about your experience. it's very simple about program logic.
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.
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.