duanan5940 2018-04-03 02:35
浏览 82
已采纳

isset检查,空检查和初始化后的变量未定义

I have a control structure that generates a footer based on the request uri, but I am getting a bad bug that says:

Notice: Undefined variable: modalResponse in C:\xampp\htdocs\SoftwareAPIs\tribeca-app\foot.php on line 25

Notice: Undefined variable: modalResponse in C:\xampp\htdocs\SoftwareAPIs\tribeca-app\foot.php on line 26

But here is what foot.php looks like:

<?php 
    if ($_SERVER["REQUEST_URI"] === "/tribeca-app/home/") {
        echo "<footer id='homeFooter'>

        <p>Created by <a href='https://mcgurkconsulting.com' title='McGurk Consulting'>Adam McGurk</a> &copy; <span id='currentYear'></span> </p>

        </footer>

        <script src='/tribeca-app/js/bundle.js'></script>
        <script src='index.js'></script>
        if (isset($modalResponse)) {
            echo $modalResponse;
        }
        </body>
        </html>";
    } else {
        echo "<footer>

        <p>Created by <a href='https://mcgurkconsulting.com' title='McGurk Consulting'>Adam McGurk</a> &copy; <span id='currentYear'></span> </p>

        </footer>

        <script src='/tribeca-app/js/bundle.js'></script>
        <script src='index.js'></script>
        if (isset($modalResponse)) {
            echo $modalResponse;
        }
        </body>
        </html>";
    }

?>

The view that includes the foot.php is served by my index.php control, and that is where the variable is defined. I just don't understand why my isset() isn't working. Here is the index.php control:

<?php

// Dependencies
session_start();
require_once "../db.php";
require_once "model.php";

// Serve the view if it is navigated to
if (empty($_POST) && empty($_GET) && empty(file_get_contents("php://input"))) {
    $pageTitle = "Register";
    include "../view/register.php";
    exit;
}

// Receive and filter input
$name = strval(filter_input(INPUT_POST, "name", FILTER_SANITIZE_STRING));
$username = filter_input(INPUT_POST, "username", FILTER_SANITIZE_EMAIL);
$password = filter_input(INPUT_POST, "password", FILTER_SANITIZE_STRING);

// Server side validation
if ($name === "") {
    $modalResponse = "<script>showModal(`You must fill out your name.`)</script>";
    exit;
} if ($username === "") {
        $modalResponse = "<script>showModal(`You must fill out your email.`)</script>";
        exit;
} if ($password === "") {
        $modalResponse = "<script>showModal(`You must provide a password.`)</script>";
        exit;
} if (!filter_var($username, FILTER_VALIDATE_EMAIL)) {
        $modalResponse = "<script>showModal(`You must provide a valid email.`)</script>";
        exit;
}

// Check if user already exists
if (checkUser($username)) {
    // username already exists
}

// Hash password and insert into database
$password = password_hash($password, PASS_ALGO);
$result = registerUser($name, $username, $password);

if ($result === 1) {
    $_SESSION["modalResponse"];
} else {
    $modalResponse;
}

Why is my isset not preventing my php errors?

  • 写回答

4条回答 默认 最新

  • duanbei7005 2018-04-03 02:40
    关注

    Your isset isn't being evaluated as php, only modalResponse is. It is because everything in the double quotes that is not a php variable name is treated as a string.

    To evaluate the statement as PHP, try something like:

     <script src='index.js'></script>
     ". if (isset($modalResponse)) {
            echo $modalResponse;
     } . "
     </body>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?