- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在运行 PHP 脚本并继续收到如下错误:
Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php on line 10
Notice: Undefined index: my_index C:\wamp\www\mypath\index.php on line 11
Warning: Undefined array key "my_index" in C:\wamp\www\mypath\index.php on line 11
echo "My variable value is: " . $my_variable_name;
echo "My index value is: " . $my_array["my_index"];
这些错误信息的含义是什么?
This is a General Reference question for people to link to as duplicate, instead of having to explain the issue over and over again. I feel this is necessary because most real-world answers on this issue are very specific.
Related Meta discussion:
最佳答案
注意/警告: undefined variable
来自PHP Manual的博大智慧:
Relying on the default value of an uninitialized variable is problematic in the case of including one file into another which uses the same variable name. It is also a major security risk with register_globals turned on. E_NOTICE level error is issued in case of working with uninitialized variables, however not in the case of appending elements to the uninitialized array. isset() language construct can be used to detect if a variable has been already initialized. Additionally and more ideal is the solution of empty() since it does not generate a warning or error message if the variable is not initialized.
No warning is generated if the variable does not exist. That meansempty() is essentially the concise equivalent to !isset($var) || $var== false.
empty()
确定是否设置了变量,此外它还根据以下内容检查变量,
0
,
0.0
,
""
,
"0"
,
null
,
false
或
[]
.
$o = [];
@$var = ["",0,null,1,2,3,$foo,$o['myIndex']];
array_walk($var, function($v) {
echo (!isset($v) || $v == false) ? 'true ' : 'false';
echo ' ' . (empty($v) ? 'true' : 'false');
echo "\n";
});
在
3v4l.org online PHP editor 中测试上述代码段
E_NOTICE
,默认情况下甚至不报告,但手册
advises to allow在开发过程中。
isset()
/ !empty()
在引用它们之前检查它们是否已声明,如://Initializing variable
$value = ""; //Initialization value; Examples
//"" When you want to append stuff later
//0 When you want to add numbers later
//isset()
$value = isset($_POST['value']) ? $_POST['value'] : '';
//empty()
$value = !empty($_POST['value']) ? $_POST['value'] : '';
// Null coalesce operator - No need to explicitly initialize the variable.
$value = $_POST['value'] ?? '';
set_error_handler('myHandlerForMinorErrors', E_NOTICE | E_STRICT)
E_NOTICE
的方法是:error_reporting( error_reporting() & ~E_NOTICE )
isset()
或 array_key_exists()
://isset()
$value = isset($array['my_index']) ? $array['my_index'] : '';
//array_key_exists()
$value = array_key_exists('my_index', $array) ? $array['my_index'] : '';
list()
当它试图访问一个不存在的数组索引时可能会产生这个:list($a, $b) = array(0 => 'a');
//or
list($one, $two) = explode(',', 'test string');
0
,所以这将产生:
Notice: Undefined offset: 1
$_POST
/
$_GET
/
$_SESSION
多变的
$_POST
时,上面的通知经常出现。 ,
$_GET
或
$_SESSION
.对于
$_POST
和
$_GET
您只需要在使用它们之前检查索引是否存在。对于
$_SESSION
您必须确保 session 以
session_start()
开始并且该索引也存在。
关于php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43616134/
我正在运行PHP脚本,并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php o
我正在运行PHP脚本,并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php o
我正在运行PHP脚本,并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php o
我正在运行PHP脚本,并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php o
我正在运行PHP脚本,并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php o
我正在运行PHP脚本,并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php o
我正在运行PHP脚本,并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php o
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我正在运行 PHP 脚本并继续收到如下错误: Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php
我是一名优秀的程序员,十分优秀!