How do I perform an additional action only on the first iteration of a loop?
loops, php
Solution
foreach ($arr as $i => $val)
{
if ($i == 0) { doSomethingSpecial(); } // Only happens once.
doSomething(); // Happens every time.
}
Problem
I know how to do this... I'll give example code below. But I can't shake the feeling that there's a clever way to accomplish what I want to do without using a variable like `$isfirstloop`. ``` $isfirstloop = true; foreach($arrayname as $value) { if ($isfirstloop) { dosomethingspecial(); $isfirstloop = false; } dosomething(); } ``` Is there any way to execute `dosomethingspecial()` only on the first loop, while executing `dosomething()` on every loop, without introducing a variable like `$isfirstloop`? Thanks!