我正在运行一个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;
}
这是因为变量'$user_location'没有被定义。如果你在里面使用任何If循环,你声明了'$user_location'变量,那么你也必须有一个else循环并定义相同的。例如:
$a = 10;
if($a == 5) {
$user_location = 'Paris';
}
else {
}
echo $user_location;
上面的代码将创建一个错误,因为if循环不满足,并且在else循环中没有定义“$user_location”。PHP仍然被要求回显变量。所以要修改代码,你必须做到以下几点:
$a = 10;
if($a == 5) {
$user_location='Paris';
}
else {
$user_location='SOMETHING OR BLANK';
}
echo $user_location;
使用三元运算符简单、易读、干净:
Pre PHP 7
将一个变量赋给另一个变量的值,否则赋null(或任何你需要的默认值):
$newVariable = isset($thePotentialData) ? $thePotentialData : null;
PHP 7 +。
除了使用空合并运算符外,都是一样的。不再需要调用isset(),因为它是内置的,也不需要提供要返回的变量,因为它假设返回正在检查的变量的值:
$newVariable = $thePotentialData ?? null;
这两者都将阻止OP的问题的通知,并且两者都完全等价于:
if (isset($thePotentialData)) {
$newVariable = $thePotentialData;
} else {
$newVariable = null;
}
如果你不需要设置一个新变量,那么你可以直接使用三元操作符的返回值,例如使用echo,函数参数等:
回音:
echo 'Your name is: ' . isset($name) ? $name : 'You did not provide one';
功能:
$foreName = getForeName(isset($userId) ? $userId : null);
function getForeName($userId)
{
if ($userId === null) {
// Etc
}
}
上面的方法同样适用于数组,包括会话等,将检查的变量替换为:
$ _SESSION(“checkMe”)
或者取决于你需要多少层深度,例如:
美元的客户(“个人”)(“地址”)(“邮政编码”)
抑制:
可以用@屏蔽PHP通知或降低错误报告级别,但这并不能解决问题。它只是停止在错误日志中报告它。这意味着您的代码仍然试图使用一个未设置的变量,这可能意味着某些事情没有按照预期的方式工作——这取决于缺少的值有多重要。
您确实应该检查这个问题并适当地处理它,或者提供不同的消息,或者甚至只是为其他所有内容返回一个空值,以确定准确的状态。
如果您只关心通知不在错误日志中,那么可以选择忽略错误日志。
HTML表单提交后变量不存在的一个常见原因是表单元素没有包含在<form>标记中:
示例:<表单>中不包含的元素
<form action="example.php" method="post">
<p>
<input type="text" name="name" />
<input type="submit" value="Submit" />
</p>
</form>
<select name="choice">
<option value="choice1">choice 1</option>
<option value="choice2">choice 2</option>
<option value="choice3">choice 3</option>
<option value="choice4">choice 4</option>
</select>
示例:元素现在包含在<表单>中
<form action="example.php" method="post">
<select name="choice">
<option value="choice1">choice 1</option>
<option value="choice2">choice 2</option>
<option value="choice3">choice 3</option>
<option value="choice4">choice 4</option>
</select>
<p>
<input type="text" name="name" />
<input type="submit" value="Submit" />
</p>
</form>
我问了一个关于这个问题的问题,我被推荐到这篇文章,上面写着:
这个问题已经有了答案:
“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)){
我不想禁用通知功能,因为它很有帮助,但我想避免太多的输入。
我的解是这个函数:
function ifexists($varname)
{
return(isset($$varname) ? $varname : null);
}
因此,如果我想引用$name和echo if存在,我简单地写:
<?= ifexists('name') ?>
对于数组元素:
function ifexistsidx($var,$index)
{
return(isset($var[$index]) ? $var[$index] : null);
}
在一个页面中,如果我想引用$_REQUEST['name']:
<?= ifexistsidx($_REQUEST, 'name') ?>