18

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?

stevenmc
  • 1,659
  • 4
  • 18
  • 27

5 Answers5

27

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.

Elzo Valugi
  • 27,240
  • 15
  • 95
  • 114
13

Yes, this is an infinite loop.

The explicit version would be

while (true == true)
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • Yep, true will always remain true and not change state so your loop will never end. – Scoop Nov 25 '10 at 12:11
  • 2
    @Ignacio you mean signed vs. unsigned `true`. Those are coming in PHP 7, I'm told – Pekka Nov 25 '10 at 12:12
  • 9
    Not explicit enough. `while (((((true == true) == true) == true) == true) == true){` – Ignacio Vazquez-Abrams Nov 25 '10 at 12:14
  • 2
    and the inverse: `while (true != false)` and back: `while ((true != false )==true)` – bcosca Nov 25 '10 at 12:21
  • 1
    @stillstanding One would have to add your code to @Ignacio's to be 100% sure. It's because of PHP's weak typing that such checks need to be reinforced a few times for it to be really stable. – Pekka Nov 25 '10 at 12:22
  • also `while (constant("TRUE"))` and clever variations with `isset`, and btw you need the `===` exact match operator – mario Nov 25 '10 at 12:25
  • you might need to do some rounding and tolerance checking too, in case TRUE returns a float – bcosca Nov 25 '10 at 12:25
  • 1
    Think different: `while(!isset($unsetvariable)){}` – acm Nov 25 '10 at 12:26
  • @stillstanding True. It will do that when the bcmath extension is installed, so one can get fractions of `true` – Pekka Nov 25 '10 at 12:27
  • 1
    @andre: `while (extension_loaded('Core')) {}` – bcosca Nov 25 '10 at 12:30
  • 1
    @stillstanding: lol was thinking something similar... `while(function_exists('function_exists')){}` – acm Nov 25 '10 at 12:30
  • 1
    this comment section now qualifies as the "Most Stupid Code Golf" – bcosca Nov 25 '10 at 12:31
  • @andre you can `continue`, but you can never `break` – Pekka Nov 25 '10 at 12:36
  • `goto first_comment; // endless loop` – bcosca Nov 25 '10 at 12:51
  • 1
    @stillstanding this comment thread supports PHP 5.2 only. It's my answer, so I get to choose the version. No `goto` on my watch – Pekka Nov 25 '10 at 12:52
  • 5
    I do not see why (true == true) should be more explicit than (true) – Marco Altieri Dec 24 '15 at 10:55
  • @Marco "explicit" meaning, "human readable so you can understand what the interpreter is doing" – Pekka Dec 24 '15 at 10:57
  • 2
    @Pekka, given that at the end the result is a boolean expression, it seems to me that the expression "true" (the most readable boolean expression possible) is better than "true == true". Using the same approach, If you need the expression false what would you write ? false == true ? – Marco Altieri Dec 24 '15 at 11:02
  • @Marco `true`, in this context, is not as easy to understand to a novice as `true == true`. `Using the same approach, If you need the expression false what would you write ? false == true?` when explaining to someone how programming works, in this context, yes. – Pekka Dec 24 '15 at 11:37
  • @FarisRayhan well, it *is* an infinite loop. The script will never terminate and you probably won't see a response. – Pekka Nov 05 '17 at 08:15
5

Please referes to the PHP documentation currently at: 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 (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.

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.

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

while (true) {
    statement(s)
    if (condition) {
        break;
    }
}
Marco Altieri
  • 3,726
  • 2
  • 33
  • 47
  • w3schools is a third-party site hosting educational content, not the php documentation - the official documentation can be found at https://www.php.net (https://www.php.net/manual/en/control-structures.while.php) – TheThirdMan Mar 18 '22 at 12:13
5

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

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)

Gekkie
  • 996
  • 9
  • 24
3

It is indeed an infinite loop.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358