PHP 将分配的变量设置为未定义 - 它保持不变吗?
我对我刚刚调试的某些行为有疑问,特别是如果已设置的变量被分配给未定义的值会发生什么。我只是想检查我是否正确理解发生了什么。如果一个变量已经设置了值,并且您尝试将其设置为未定义的值,那么它会保留其旧值吗?
具体来说,我有一些 PHP 代码,看起来大约像这样 - 假设 $string 是一些由 1 和 2 组成的字符串。
$array = array(1 => 'foo', 2 => 'bar');
for($count=0;$count<len($string);$count++)
{
$newvar = $array[$string[$count]];
if(!empty($newvar))
{
switch($newvar)
{
case 'foo':
// blah blah break;
case 'bar':
// blah blah break;
}
}
}
现在,我的代码应该将 $string 设置为类似“12212”的内容,但我的一个错误是向它发送末尾带有额外空格的内容 - “12212”。这导致了一些异常行为,我认为发生的事情是这样的 - 当 $count=5 时,$string[5] 未定义,因此 $array[$string[5]] 未定义,$newvar 保持为 2。因此我的if(!empty 语句没有完成其工作,并且 case 'bar' 发生的次数比应有的次数多。这一切看起来像会发生什么吗?
当然,修剪 $string 解决了我的问题,但我想确保我明白出了什么问题。如果这是一个愚蠢的问题,我深表歉意 - 我只是一个业余爱好者......
upstr 应该是一串数字。
$len = strlen($upstr);
$cost=0;
$upnames = array(4=>"man", 2=>"raw", 1=>"food", 3=>"fuel",5=>"tech");
for($strloop=0;$strloop<$len; $strloop++)
{
$number = $upstr[$strloop];
if(! empty($number))
{
$name = $upnames[$number];
$cost+= mysql_result($result1,0,$name) +1;
if(mysql_result($result2,0,$name."up")==1)
{
$cost+=100;
}
}
}
编辑:这是实际的代码$ 末尾的额外空格是我会看到一个 mysql 错误,它无法在 $result2 中找到“up”列,因此它试图在 if() 语句中运行该代码块,而 $name 为空或。如果我故意添加 3 或 4 个额外的空格,我会看到很多 mysql 错误。
I have question about some behavior I was just debugging, specifically what happens if a variable which is already set is assigned to an undefined value. I just want to check that I'm understanding what happened correctly. If a variable has a value set already, and you try to set it to something undefined, it stays at its old value?
Specifically, I had some PHP code that looked approximately like this - assume that $string is some string of 1's and 2's.
$array = array(1 => 'foo', 2 => 'bar');
for($count=0;$count<len($string);$count++)
{
$newvar = $array[$string[$count]];
if(!empty($newvar))
{
switch($newvar)
{
case 'foo':
// blah blah break;
case 'bar':
// blah blah break;
}
}
}
Now, my code was supposed to set $string to be something like "12212", but an error on my part was sending it something with extra spaces at the end - "12212 ". This caused some aberrant behavior, and I think what happened was this - when $count=5, $string[5] is undefined, so $array[$string[5]] is undefined, and $newvar stays as 2. Thus my if(!empty statement doesn't do its job and case 'bar' happens more times than it should have. Does that all seem like what would happen?
Of course, trimming $string solved my problem, but I want to make sure I understand what was going wrong. Apologies if this is a stupid question - I'm just an amateur here....
Edit: Here's the actual code. $upstr is supposed to be a string of digits.
$len = strlen($upstr);
$cost=0;
$upnames = array(4=>"man", 2=>"raw", 1=>"food", 3=>"fuel",5=>"tech");
for($strloop=0;$strloop<$len; $strloop++)
{
$number = $upstr[$strloop];
if(! empty($number))
{
$name = $upnames[$number];
$cost+= mysql_result($result1,0,$name) +1;
if(mysql_result($result2,0,$name."up")==1)
{
$cost+=100;
}
}
}
What happened when $upstr had some extra spaces at the end was I would see a mysql error, that it couldn't find the column "up" in $result2 . So it was trying to run that block of code in the if() statement with $name being empty or NULL or something. And if I intentionally added 3 or 4 extra spaces, I would see that many mysql errors.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
恐怕您的代码示例中变量 $array 的定义不正确,它应该如下所示:
$array = array(1 => 'foo', 2 => 'bar');
如果将 $newvar 设置为 $array 的未定义元素(例如 3),则 $newvar 将设置为 NULL。
I'm afraid the definition of variable $array is incorrect in your code example, it should read as follows:
$array = array(1 => 'foo', 2 => 'bar');
If you set $newvar to an undefined element of $array (e.g. 3) then $newvar will be set to NULL.
使用 array_key_exists($array, $string[$count]) 来检查您的数组是否具有您的键值。
Use
array_key_exists($array, $string[$count])
to check if your array has value for your key.好的,我已经弄清楚是什么导致了我所看到的行为。字符串 '' 和 ' ' 在 empty() 下的行为不同。其中一个被认为是空的,而另一个则不是,这让我感到困惑。非常感谢您的所有帮助。
Ok, I've figured out what was causing the behavior I saw. The strings '' and ' ' behave differently under empty(). One of them is considered empty and the other isn't, which was confusing me. Thanks so much for all the help.