Home > Article > Backend Development > PHP debugging encounters Invalid argument supplied for foreach()
1. Preparation before use:
Basic syntax of is_array():
bool is_array ( mixed $var )
The is_array() function is used to detect whether a variable is aArray
.
$var: Variable to be detected
If the variable being detected is an array, return TRUE
, otherwise return FALSE
.
Usage demonstration:
<?php $arr_site = array('PHP', 'JAVA', 'C#'); if(is_array($arr_site)){ echo '变量 $arr_site 是一个数组'; } else { echo '变量 $arr_site 不是一个数组'; } ?>
The output result is: the variable $arr_site
is an array.
2. Error reason:
When using foreach
in php
to loop through times Invalid argument supplied for foreach() The error is because the looped data is not a valid array. We can use is_array()
to determine the data source before foreach
.
if(is_array($data)) { foreach($data as $value) {...} }
Recommended: "php video tutorial" "php tutorial"
The above is the detailed content of PHP debugging encounters Invalid argument supplied for foreach(). For more information, please follow other related articles on the PHP Chinese website!