This is a strange question, but it's interesting so I thought I'd ask. Let's say I don't like how php's foreach
loop operates and I always use it in the format, where all I did is added a counter outside and used it inside the foreach
loop to tell if we're in the last element.
$counter = 1;
$array_size = count($array);
foreach($array as $item){
//code
if($counter == $array_size){
$isLast = true
} else {
$isLast = false;
}
$counter++;
}
My question is: can I override or fork or inherit (or whatever you want to call that) the original php foreach loop to include this new ability, so that I can now do this, without the need for initializing the counter and such.
newforeach($array as $item){
//code
$isLast = //something that returns true/false;
}
It's an interesting idea, so I wonder if one of the hardcore php guys here can somehow make this possible? or this sort of modification way too complicated without hacking the language itself?!