MrZ MrZ - 1 month ago 5
PHP Question

What's the meaning of a semicolon inside a while loop?

I'm trying to understand this PHP Snippet

public function GetString(){
$point = $this->bytePointer;
while($this->GetUInt8() != 0){
;
}

return substr($this->raw, $point, $this->bytePointer - 1 - $point);
}


what's the meaning of the ";" inside the
while()
loop?

Answer

This is just a no-op. The loop has an empty body.

It was possibly included to improve readability. A comment could have made it even more obvious:

while (...) {
    // do nothing in the loop body
}

You could include any number of semicolons after a statement (which I do not recommend).

All of the following define an empty loop body:

  1. while (...) {
        ;
    }
    
  2. while (...) {}  // With any whitespace / newlines within the braces
    
  3. while (...); // Note that the semicolon is REQUIRED in case of missing braces! 
    
  4. while (...) {
        ;;;;;;
        // any number 
        /* and type */
        # of comments 
    }
    
Comments