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条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改