php, infinite loop in while() loop

后端 未结 3 2048
梦毁少年i
梦毁少年i 2021-01-15 15:27
/// infinite loop??
$x=1; 
while($x=9){ 
echo $x;
$x++;
}

i dont understand the reason behind, why the above code causes infinite loop in my opinio

相关标签:
3条回答
  • 2021-01-15 15:43

    $x=9 is an assignment, and is always true. Perhaps you meant $x==9, or some other relational operator.

    0 讨论(0)
  • 2021-01-15 15:53

    You are assigning the value of 9 to the variable x instead of performing a relational comparison. A common mistake. = is the assignment operator whereas == is the equality comparison operator.

    http://en.wikipedia.org/wiki/Assignment_(computer_science)#Assignment_versus_equality

    0 讨论(0)
  • 2021-01-15 15:57

    You mean

    $x == 9
    

    But in your example it won't do anything, because $x != 9. You probably mean

    while($x < 9)
    
    0 讨论(0)
提交回复
热议问题