带有 $

编程入门 行业动态 更新时间:2024-10-27 19:19:10
本文介绍了带有 $_POST 的未定义索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试重新学习一些 PHP 基础知识来制作一个简单的登录脚本,但是我收到了一个我以前没有收到过的错误(我一年多前制作了相同的脚本,但从未出现过这个错误.我简化了代码尽可能多地测试以查看哪个区域有问题,这是问题所在:

I am trying to relearn some PHP basics for making a simple login script, however I get an error I have not received before(I made the same script a little over a year ago and never had this error. I simplified the code as much as I could to test to see which area was problematic and here is the issue:

<?php $user = $_POST["username"]; if($user != null) { echo $user; echo " is your username"; } else { echo "no username supplied"; } ?>

现在,当我向脚本发送一个变量时,这段代码可以正常工作,但是当没有提供变量时,它会抛出一个错误.理论上这会很好,因为如果没有提供用户名/通行证,则会出现错误.在将代码发送到脚本之前,我将进行检查以确保这一点,但是我担心以某种方式可能会泄漏一个空白字符串并吐出一些未知错误.这是我得到的错误:

Now this code works fine when I send a variable to the script, but when no variable is supplied it spits out an error. In theory this will be fine because if no username/pass is supplied then an error is expected. I will be checking to make sure of this before the code is send to the script, however I fear that somehow a blank string may leak through and spit out some unknown error. Here is the error I get:

( ! ) Notice: Undefined index: username in C:wampwwwverify_login.php on line 2 Call Stack Time Memory Function Location 1 0.0003 668576 {main}( ) ..verify_login.php:0

未提供用户名

正如你所看到的代码寄存器没有提供变量,但它给出了错误,我认为这意味着找不到变量是预期的变量或类似的东西.有人可以帮我澄清一下吗?

as you can see the code registers that no variable was supplied, but it gives out and error that I assume means that a variable was not found were one was expected or something like that. Can someone please clarify this for me?

推荐答案

在 PHP 中,从未设置过的变量或数组元素与值为 null 的变量或数组元素不同;试图访问这样一个 unset 值是一个运行时错误.

In PHP, a variable or array element which has never been set is different from one whose value is null; attempting to access such an unset value is a runtime error.

这就是您遇到的情况:数组 $_POST 在键 "username" 处没有任何元素,因此解释器会在它之前中止您的程序进入无效测试.

That's what you're running into: the array $_POST does not have any element at the key "username", so the interpreter aborts your program before it ever gets to the nullity test.

幸运的是,您无需实际尝试访问即可测试变量或数组元素是否存在;这就是特殊运算符 isset 的作用:

Fortunately, you can test for the existence of a variable or array element without actually trying to access it; that's what the special operator isset does:

if (isset($_POST["username"])) { $user = $_POST["username"]; echo $user; echo " is your username"; } else { $user = null; echo "no username supplied"; }

当 PHP 尝试获取 $_POST["username"] 的值作为参数传递给函数时,它看起来会以与您的代码完全相同的方式爆炸isset().然而,isset() 根本不是真正的函数,而是在求值阶段之前识别的特殊语法,因此 PHP 解释器会检查该值是否存在,而无需实际尝试检索它.

This looks like it will blow up in exactly the same way as your code, when PHP tries to get the value of $_POST["username"] to pass as an argument to the function isset(). However, isset() is not really a function at all, but special syntax recognized before the evaluation stage, so the PHP interpreter checks for the existence of the value without actually trying to retrieve it.

还值得一提的是,随着运行时错误的发生,丢失的数组元素被视为次要元素(分配了 E_NOTICE 级别).如果您更改 error_reporting 级别以便忽略通知,您的原始代码实际上将按编写的方式工作,尝试的数组访问返回 null.但这被认为是不好的做法,尤其是对于生产代码.

It's also worth mentioning that as runtime errors go, a missing array element is considered a minor one (assigned the E_NOTICE level). If you change the error_reporting level so that notices are ignored, your original code will actually work as written, with the attempted array access returning null. But that's considered bad practice, especially for production code.

附注:PHP 会进行字符串插值,因此 if 块中的 echo 语句可以合并为一个:

Side note: PHP does string interpolation, so the echo statements in the if block can be combined into one:

echo "$user is your username";

更多推荐

带有 $

本文发布于:2023-10-11 17:40:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1482378.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!