Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

Keyword while

Check the condition before every iteration, including the first.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

while ($condition) { /* body */ }

Check the condition before every iteration, including the first. A false initial condition skips the body entirely. When processing values that may be false-like, test the termination marker explicitly so a valid zero or empty string does not stop the loop early.

$items=[0,"",2]; $i=0; $seen=[];
while(array_key_exists($i,$items)){ $seen[]=$items[$i++]; }
echo json_encode($seen);
[0,"",2]

Examples

<?php
$items=[0,"",2]; $i=0; $seen=[];
while(array_key_exists($i,$items)){ $seen[]=$items[$i++]; }
echo json_encode($seen);

Result

[0,"",2]

Sources