首页 > 解决方案 > 我的代码中有错误 - count() 参数

问题描述

我的程序中有以下代码,但是当我以我无法解决的“index.php”形式包含“errors.php”时,它给了我一个错误。我以前用过它,我从来没有遇到过这个错误,所以我对这个错误感到困惑。这是我的代码:

索引.php

<form method="post" action="register.php">
    <?php include('errors.php'); ?>
    <div class="input-group">
        <label>Título</label>
        <input type="title" name="title" value="">
    </div>
    <div class="input-group">
        <label>Difficulty</label>
        <select name="difficulty" id="difficulty">
            <option value="1">1</option>
            <option value="2">2</option>
        </select>
    </div>
    <br>
    <div class="input-group">
        <label>Solución</label>
        <textarea name="solution" rows="4" cols="55"></textarea>
    </div>

    <div class="input-group">
        <button type="submit" class="btn" name="reg_exercise">Add</button>
    </div>
</form>

错误.php

<?php
if (isset($_SESSION["errors_reg"])) {
    $errors = $_SESSION["errors_reg"];
    $_SESSION["errors_reg"] = null;
}
?>

<?php
if (isset($_SESSION["errors_student"])) {
    $errors = $_SESSION["errors_student"];
    $_SESSION["errors_student"] = null;
}
?>

<?php  if (count($errors) > 0) : ?>
    <div class="error">
        <?php foreach ($errors as $error) : ?>
            <p><?php echo $error ?></p>
        <?php endforeach ?>
    </div>
<?php  endif ?>
<?php
$errors = null;
?>

我收到一条错误消息 <?php if (count($errors) > 0) : ?>

谁能告诉我为什么会出现这个错误?这是错误:

Warning: count(): Parameter must be an array or an object that implements Countable in C:\wamp64\www\project\errors.php on line 15  <?php  if (count($errors) > 0) : ?>

标签: php

解决方案


警告明确指出变量$errors应该是arrayor object,您需要更改代码

$errors[] = $_SESSION["errors_reg"];

$errors[] = $_SESSION["errors_student"];

推荐阅读