Php 如何在循环之外获取while循环值

Php 如何在循环之外获取while循环值,php,variables,while-loop,output,Php,Variables,While Loop,Output,我试图从cookie中获取值,然后使用explode()转义所有逗号,并使用while循环从cookie中循环所有值 当我尝试使用下面的代码显示循环中的值时,它起作用了: echo $hey= '<span>'.$result[$counter].'</span> '; echo$hey='.$result[$counter].'; 但是我需要访问循环外部的值,而这段代码不提供任何输出 $cookie_value=$_COOKIE["chords"]; $cou

我试图从cookie中获取值,然后使用
explode()
转义所有逗号,并使用
while
循环从cookie中循环所有值

当我尝试使用下面的代码显示
循环中的值时,它起作用了:

echo $hey= '<span>'.$result[$counter].'</span>  ';
echo$hey='.$result[$counter].';
但是我需要访问循环外部的值,而这段代码不提供任何输出

$cookie_value=$_COOKIE["chords"];  
$counter = 0;
$result=explode(",", $cookie_value);

$array_el_lenght = count($result);

while ($counter<=$array_el_lenght) {
  $hey= '<span>'.$result[$counter].'</span>  ';
  $counter++;
}

echo $hey;
$cookie\u value=$\u cookie[“和弦”];
$counter=0;
$result=explode(“,”,$cookie\u值);
$array\u el\u lenght=计数($result);

while($counter您的while循环循环次数过多,因为数组的最后一个元素位于索引
$array\u el_lengh-1
。此外,不要忘记连接结果(编辑:我只是猜测这是您要做的),而不仅仅是重新分配
$hey
!-)

试试这个:

$cookie_value=$_COOKIE["chords"];  
$counter = 0;
$result=explode(",", $cookie_value);

$array_el_lenght = count($result);

$hey = "";
while ($counter < $array_el_lenght) {
  $hey .= '<span>'.$result[$counter].'</span>  ';
  $counter++;
}
echo $hey;

$cookie\u value=$\u cookie[“和弦”];
$counter=0;
$result=explode(“,”,$cookie\u值);
$array\u el\u lenght=计数($result);
$hey=“”;
而($计数器<$阵列长度){
$hey.=''.$result[$counter].';
$counter++;
}
回声$hey;

您的while循环循环次数过多,因为数组的最后一个元素位于索引
$array\u el\u lenght-1
。另外,不要忘记连接结果(编辑:我只是猜测这就是您要做的),而不仅仅是重新分配
$hey
!;-)

试试这个:

$cookie_value=$_COOKIE["chords"];  
$counter = 0;
$result=explode(",", $cookie_value);

$array_el_lenght = count($result);

$hey = "";
while ($counter < $array_el_lenght) {
  $hey .= '<span>'.$result[$counter].'</span>  ';
  $counter++;
}
echo $hey;

$cookie\u value=$\u cookie[“和弦”];
$counter=0;
$result=explode(“,”,$cookie\u值);
$array\u el\u lenght=计数($result);
$hey=“”;
而($计数器<$阵列长度){
$hey.=''.$result[$counter].';
$counter++;
}
回声$hey;

是在循环中定义的,因此其作用域以循环结束。如果要从循环外部访问它,也应该在循环外部定义它:

$hey = NULL;
while ($counter<=$array_el_lenght) {
    $hey= '<span>'.$result[$counter].'</span>  ';
    $counter++;
}
if ($hey) {
    echo $hey;
}
$hey=NULL;

而($counter
$hey
是在循环内部定义的,因此其作用域以循环结束。如果要从循环外部访问它,也应该在循环外部定义它:

$hey = NULL;
while ($counter<=$array_el_lenght) {
    $hey= '<span>'.$result[$counter].'</span>  ';
    $counter++;
}
if ($hey) {
    echo $hey;
}
$hey=NULL;

当($counter您试图访问未定义的偏移量
$result
时,您的代码试图访问不存在的
$result[count($result)]
,您需要替换while语句,因此在上一次迭代中,索引位于
count($result)-1


当($counter<$array\u el_lengh){//Replaced您试图访问未定义的偏移量
$result
,您的代码试图访问不存在的
$result[count($result)]
时,您需要替换while语句,因此在上一次迭代中,索引位于
count($result)-1


while($counter<$array\u el\u length){//Replaced也不会给出结果,或者会出现错误。请看@mbinette answers。在他的示例中,$key是空的,因为他试图访问一个未定义的偏移量,因为他迭代了array_length+1次,@Mureinik answer是错误的,因为他也这么做了,while语句是错误的。这是问题的真正答案。它是“不是因为像其他人在这里说的那样的范围问题,而是因为未定义的偏移量。始终启用错误报告,以便在开发时可以看到任何致命错误。@Georgaldc谢谢,我得到了几张反对票,不知道为什么,当其他答案做同样的事情时,它们都有未定义的偏移量。这很有趣b。”因为他们都在讨论与问题无关的范围。也不会给出结果,或者会有错误。请看@mbinette answers。在他的示例中,$key是空的,因为他试图访问一个未定义的偏移量,因为他迭代了array_length+1次,@Mureinik answer是错误的,因为他也这么做,所以while语句是错误的。这是问题的真正答案。这不是因为像其他人在这里所说的范围问题,而是因为未定义的偏移量。始终启用错误报告,以便您在开发时可以看到任何致命错误。@georaldc谢谢,我得到了几张反对票,不知道为什么,当其他答案被删除时做同样的事情,它们都有一个未定义的偏移量。这很有趣,因为它们都在谈论范围,而这与问题无关。如果缺少分号,则代码会抛出语法错误:$hey=“”;)@mbinette在您提供的PHP文档链接中没有说明循环有自己的局部作用域。如果OP只是做了一个小于的比较,假设她没有将值连接到$hey,那么她会用echo语句返回一些内容。我已经更正了上面的帖子。我会删除它,但我不能,因为这是公认的答案。我已经你缺少一个分号,该代码抛出一个语法错误:$hey=“”;)@mbinette在您提供的PHP文档链接中没有说明循环有自己的局部作用域。如果OP只是做了一个小于的比较,假设她没有将值连接到$hey,那么她会用echo语句返回一些内容。我已经更正了上面的帖子。我会删除它,但我不能,因为这是公认的答案。我已经奥德·马科斯的回答是正确的,希望OP接受我的回答,接受他的回答。