How to make sure the value is reset in foreach loop in PHP
Posted
by kwokwai
on Stack Overflow
See other posts from Stack Overflow
or by kwokwai
Published on 2010-04-28T10:45:30Z
Indexed on
2010/04/28
10:53 UTC
Read the original article
Hit count: 193
php5
Hi all,
I was writing a simple PHP page and a few foreach loops were used.
Here are the scripts:
$arrs = array("a", "b", "c");
foreach ($arrs as $arr) {
if(substr($arr,0,1)=="b")
{
echo "This is b";
}
} // ends of first foreach loop and I didn't use ifelse here
And when this foreach ends, I wrote another foreach loop in which all the values in the foreach loop was the same as previous foreach.
foreach ($arrs as $arr) {
if(substr($arr,0,1)=="c")
{
echo "This is c";
}
}
I am not sure if it is a good practice to have two foreach loops with same values and keys.
Will the values get overwritten in the first foreach loop?
© Stack Overflow or respective owner