As we know foreach loop will execute till condition does not become false. I want to execute it only once.
$i=0;
foreach($html->find('img') as $element)
{
if($i!=0)
break;
$logo= $element->src . '<br>';
$i++;
}
Is there any other solution for this? like foronce
in place of foreach
?
Initialisation part of for loop executes only once. int I=0 executes only once. That i=0 statement would execute only once.
You cannot make an infinite foreach loop. foreach is specifically for iterating through a collection. If that's not what you want, you should not be using foreach .
As we know foreach loop will execute till condition does not become false.
A foreach
loop will execute once for every item of the container unless a return
or break
condition is defined within the foreach
block.
I want to execute it only once.
Loops that executes once are called "don't use a loop in the first place". Here's your example fixed:
$logo = $html->find('img')[0]->src . '<br>';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With