虽然(真){在PHP中意味着什么?

时间:2022-08-15 21:39:46

I've seen this code, and I've no idea what it means.

我见过这段代码,我不知道它是什么意思。

while(true){
    echo "Hello world";
}

I know what a while loop is, but what does while(true) mean? How many times will it executed. Is this not an infinite loop?

我知道什么是while循环,但while(true)是什么意思?它会被执行多少次。这不是一个无限循环吗?

5 个解决方案

#1


10  

Yes, this is an infinite loop.

是的,这是一个无限循环。

The explicit version would be

显式版本将是

while (true == true)

#2


18  

Although is an infinite loop you can exit it using break. It is useful when waiting for something to happen but you don't exactly know the number of iteration that will get you there.

虽然是无限循环,但您可以使用break退出它。它在等待某些事情发生时非常有用,但是您并不确切知道可以实现的迭代次数。

#3


5  

This is indeed (as stated already) an infinite loop and usually contains code which ends itself by using a 'break' / 'exit' statement.

这确实(如前所述)是一个无限循环,通常包含通过使用'break'/'exit'语句结束自己的代码。

Lots of daemons use this way of having a PHP process continue working until some external situation has changed. (i.e. killing it by removing a .pid file / sending a HUP etc etc)

许多守护进程使用这种方式让PHP进程继续工作,直到某些外部情况发生变化。 (即通过删除.pid文件/发送HUP等来杀死它)

#4


2  

It is indeed an infinite loop.

它确实是一个无限循环。

#5


1  

Please referes to the PHP documentation currently at: http://www.w3schools.com/php/php_looping.asp

请参考目前位于以下网址的PHP文档:http://www.w3schools.com/php/php_looping.asp

The while loop executes a block of code as long as the specified condition is true.

只要指定的条件为真,while循环就会执行一段代码。

while (expression) {
    statement(s) 
} 

The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false.

while语句计算表达式,该表达式必须返回一个布尔值。如果表达式的计算结果为true,则while语句将执行while块中的语句。 while语句继续测试表达式并执行其块,直到表达式求值为false。

As a consequence, the code:

因此,代码:

while (true) {
    statement(s)
}

will execute the statements indefinitely because "true" is a boolean expression that, as you can expect, is always true.

将无限期地执行语句,因为“true”是一个布尔表达式,正如您所料,它总是正确的。

As already mentioned by @elzo-valugi, this loop can be interrupted using a break (or exit):

正如@ elzo-valugi已经提到的,可以使用break(或exit)中断此循环:

while (true) {
    statement(s)
    if (condition) {
        break;
    }
}

#1


10  

Yes, this is an infinite loop.

是的,这是一个无限循环。

The explicit version would be

显式版本将是

while (true == true)

#2


18  

Although is an infinite loop you can exit it using break. It is useful when waiting for something to happen but you don't exactly know the number of iteration that will get you there.

虽然是无限循环,但您可以使用break退出它。它在等待某些事情发生时非常有用,但是您并不确切知道可以实现的迭代次数。

#3


5  

This is indeed (as stated already) an infinite loop and usually contains code which ends itself by using a 'break' / 'exit' statement.

这确实(如前所述)是一个无限循环,通常包含通过使用'break'/'exit'语句结束自己的代码。

Lots of daemons use this way of having a PHP process continue working until some external situation has changed. (i.e. killing it by removing a .pid file / sending a HUP etc etc)

许多守护进程使用这种方式让PHP进程继续工作,直到某些外部情况发生变化。 (即通过删除.pid文件/发送HUP等来杀死它)

#4


2  

It is indeed an infinite loop.

它确实是一个无限循环。

#5


1  

Please referes to the PHP documentation currently at: http://www.w3schools.com/php/php_looping.asp

请参考目前位于以下网址的PHP文档:http://www.w3schools.com/php/php_looping.asp

The while loop executes a block of code as long as the specified condition is true.

只要指定的条件为真,while循环就会执行一段代码。

while (expression) {
    statement(s) 
} 

The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. The while statement continues testing the expression and executing its block until the expression evaluates to false.

while语句计算表达式,该表达式必须返回一个布尔值。如果表达式的计算结果为true,则while语句将执行while块中的语句。 while语句继续测试表达式并执行其块,直到表达式求值为false。

As a consequence, the code:

因此,代码:

while (true) {
    statement(s)
}

will execute the statements indefinitely because "true" is a boolean expression that, as you can expect, is always true.

将无限期地执行语句,因为“true”是一个布尔表达式,正如您所料,它总是正确的。

As already mentioned by @elzo-valugi, this loop can be interrupted using a break (or exit):

正如@ elzo-valugi已经提到的,可以使用break(或exit)中断此循环:

while (true) {
    statement(s)
    if (condition) {
        break;
    }
}