开发者

php, infinite loop in while() loop

开发者 https://www.devze.com 2023-04-05 13:06 出处:网络
/// infinite loop?? $x=1; while($x=9){ echo $x; $x++; } i dont understand the reason behind, why the above code causes infinite loop
/// 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 opinion above code should output "9" once. but it outputs endless 999999999......

at first (when x is equal to 1) while statement is false so nothing happens, then x becomes 2 but again while statement is false;

So when x becomes 9 while statement is true so it should echo 9 then we add 1 due to x++; and it becomes 10 so while statement becomes false but as i see it doesnt becaus开发者_开发知识库e

it continues to echo 9999999.......

pls enlighten me regarding the above code. best regards.

note:i have checked the similar questions but cant find the answer for my situation thx


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


You mean

$x == 9

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

while($x < 9)


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

精彩评论

暂无评论...
验证码 换一张
取 消