php - 尝试将变量打印到 HTML 中时 undefined variable

标签 php html mysql echo

PHP

if($count == 1) {
     session_register("username");
     $_SESSION['username'] = $username;

     header("location: ../home");
  }else {
     $error = "Your Login Name or Password is invalid";
  }

HTML -- 第 43 行

<?php echo "<p class='text-danger'>$error</p>"; ?>

Notice: Undefined variable: error in directory on line 43

最佳答案

Error occurred because, when if statement is true then only under if statement block code would be executed not else block code. So $error variable did not initialized. Each variable should be initialized if you want to use it. $error variable did not initialize but you use it in HTML-- line 43. Thanks

  $error ="";
  if($count == 1) {
     session_register("username");
     $_SESSION['username'] = $username;

     header("location: ../home");
  }else {
     $error = "Your Login Name or Password is invalid";
  }

关于php - 尝试将变量打印到 HTML 中时 undefined variable ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41779216/

相关文章:

php - 如何使用服务器发送的事件关闭 Redis 发布/订阅连接

html - 我的CSS来自哪里?

mysql - 将存储过程字符串分解到临时表mysql中

php - 存储不变数据的最佳方式

html - 获取 flexbox 列以填充可用空间

PHP 条件问题数据结构

php - 简单的mysql查询来检查行是否存在

php - 如何向每个 JSON 对象添加数据 PHP

php - 在按值分配的情况下,codeigniter 中的事件记录

javascript - 单击按钮时禁用对文本框的只读