Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tnayanam's avatar

Optimizing PHP array printing using while loop

I am learning PHP and Laravel together. And I have got one array which I am printing in PHP . This is what I have tried. Can I improve or optimize this below code.

$arr = array('hello', 'my', 'world');    
$i = 0;    
while($i < count($arr))  
{
    echo $arr[$i];
    $i++;    
}
0 likes
1 reply
zachleigh's avatar
Level 47

Use foreach.

$arr = array('hello', 'my', 'world');

foreach ($arr as $item)  {
    echo $item;
} 

Please or to participate in this conversation.