我正在运行一个PHP脚本,并继续收到错误,如:
注意:未定义变量my_variable_name在C:\wamp\www\mypath\index.php第10行
注意:第11行未定义索引:my_index C:\wamp\www\mypath\index.php
警告:在C:\wamp\www\mypath\index.php第11行未定义数组键“my_index”
第10和11行是这样的:
echo "My variable value is: " . $my_variable_name;
echo "My index value is: " . $my_array["my_index"];
这些错误消息的含义是什么?
为什么他们会突然出现?我曾经使用这个脚本多年,从来没有任何问题。
我该如何修复它们?
这是一个一般参考问题,供人们链接作为副本,而不是需要一遍又一遍地解释这个问题。我觉得这很有必要,因为现实世界中关于这个问题的答案都非常具体。
相关元讨论:
如何应对重复的问题?
“参考问题”有意义吗?
我一直使用自己的有用函数exst()自动声明变量。
您的代码将是-
$greeting = "Hello, " . exst($user_name, 'Visitor') . " from " . exst($user_location);
/**
* Function exst() - Checks if the variable has been set
* (copy/paste it in any place of your code)
*
* If the variable is set and not empty returns the variable (no transformation)
* If the variable is not set or empty, returns the $default value
*
* @param mixed $var
* @param mixed $default
*
* @return mixed
*/
function exst(& $var, $default = "")
{
$t = "";
if (!isset($var) || !$var) {
if (isset($default) && $default != "")
$t = $default;
}
else {
$t = $var;
}
if (is_string($t))
$t = trim($t);
return $t;
}
当我们使用一个未设置的变量时,就会发生这些错误。
处理这些问题的最佳方法是在开发时设置错误报告。
设置错误报告:
ini_set('error_reporting', 'on');
ini_set('display_errors', 'on');
error_reporting(E_ALL);
在生产服务器上,错误报告是关闭的,因此,我们不会得到这些错误。
但是,在开发服务器上,我们可以设置错误报告。
为了消除这个错误,我们看到下面的例子:
if ($my == 9) {
$test = 'yes'; // Will produce an error as $my is not 9.
}
echo $test;
在赋值或使用变量之前,可以将变量初始化为NULL。
因此,我们可以将代码修改为:
$test = NULL;
if ($my == 9) {
$test = 'yes'; // Will produce an error as $my is not 9.
}
echo $test;
这不会干扰任何程序逻辑,即使$test没有值,也不会产生Notice。
因此,基本上,在开发时将错误报告设置为ON总是更好的。
并修复所有的错误。
在生产中,错误报告应该设置为关闭。
我问了一个关于这个问题的问题,我被推荐到这篇文章,上面写着:
这个问题已经有了答案:
“Notice: Undefined variable”、“Notice: Undefined index”、“Notice: Undefined index”
未定义的偏移量”使用PHP
我在这里分享我的问题和解决方案:
这是错误:
第154行就是问题所在。第154行是这样的
153 foreach($cities as $key => $city){
154 if(($city != 'London') && ($city != 'Madrid') && ($citiesCounterArray[$key] >= 1)){
我认为问题是我正在为变量$city写if条件,它不是键,而是$key => $city中的值。第一,你能否证实这是否是警告的原因?其次,如果这是问题所在,为什么我不能根据值编写条件呢?我需要用键来写条件吗?
更新1:问题是当执行$citiesCounterArray[$key]时,有时$key对应的是一个在$citiesCounterArray数组中不存在的键,但根据我的循环数据,情况并非总是如此。我需要的是设置一个条件,以便如果数组中存在$key,则运行代码,否则跳过它。
更新2:这是我如何通过使用array_key_exists()修复它:
foreach($cities as $key => $city){
if(array_key_exists($key, $citiesCounterArray)){
if(($city != 'London') && ($city != 'Madrid') && ($citiesCounterArray[$key] >= 1)){
在PHP中,首先需要定义变量。之后你就可以使用它了。
我们可以以一种非常有效的方式检查变量是否定义!
// If you only want to check variable has value and value has true and false value.
// But variable must be defined first.
if($my_variable_name){
}
// If you want to check if the variable is defined or undefined
// Isset() does not check that variable has a true or false value
// But it checks the null value of a variable
if(isset($my_variable_name)){
}
简单的解释
// It will work with: true, false, and NULL
$defineVariable = false;
if($defineVariable){
echo "true";
}else{
echo "false";
}
// It will check if the variable is defined or not and if the variable has a null value.
if(isset($unDefineVariable)){
echo "true";
}else{
echo "false";
}