Php 我的code-count()参数中有一个错误
我的程序中有下面的代码,但当我以“index.php”的形式包含“errors.php”时,它给了我一个无法解决的错误。我以前用过它,但我从来没有犯过这个错误,所以我对这个错误感到困惑。这是我的代码: Index.phpPhp 我的code-count()参数中有一个错误,php,Php,我的程序中有下面的代码,但当我以“index.php”的形式包含“errors.php”时,它给了我一个无法解决的错误。我以前用过它,但我从来没有犯过这个错误,所以我对这个错误感到困惑。这是我的代码: Index.php <form method="post" action="register.php"> <?php include('errors.php'); ?> <div class="input-group"> <
<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
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;
?>
蒂图罗
困难
1.
2.
索卢西翁
添加
Errors.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
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;
?>
我的行中有一个错误
有人能告诉我为什么会出现这个错误吗?这就是错误:
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) : ?>
警告:count():参数必须是在第15行的C:\wamp64\www\project\errors.php中实现可数的数组或对象
警告明确指出变量$errors
应为数组
或对象
,您需要更改代码
$errors[] = $_SESSION["errors_reg"];
及
警告明确指出变量
$errors
应为数组
或对象
,您需要更改代码
$errors[] = $_SESSION["errors_reg"];
及
由于
$errors
仅在各种if
条件内设置,因此不会始终创建它。因此,首先将默认值设置为空数组,然后在每个点添加新错误,使用[]
<?php
$errors = [];
if (isset($_SESSION["errors_reg"])) {
$errors[] = $_SESSION["errors_reg"];
$_SESSION["errors_reg"] = null;
}
?>
因为
$errors
仅在各种条件内设置,如果条件,则不会始终创建它。因此,首先将默认值设置为空数组,然后在每个点添加新错误,使用[]
<?php
$errors = [];
if (isset($_SESSION["errors_reg"])) {
$errors[] = $_SESSION["errors_reg"];
$_SESSION["errors_reg"] = null;
}
?>
不确定为什么会出现$errors[]=null代码>?@NigelRen感谢您指出,更新了答案不确定您为什么有$errors[]=null代码>?@NigelRen感谢您指出,更新了答案