Home  >  Article  >  Backend Development  >  PHP debugging encounters Invalid argument supplied for foreach()

PHP debugging encounters Invalid argument supplied for foreach()

autoload
autoloadOriginal
2021-03-23 15:03:532812browse

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(&#39;PHP&#39;, &#39;JAVA&#39;, &#39;C#&#39;);
    if(is_array($arr_site)){
        echo &#39;变量 $arr_site 是一个数组&#39;;
    } else {
        echo &#39;变量 $arr_site 不是一个数组&#39;;
    }
?>

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn