What is better handled for arrays?

although I use foreach and while,

I was wondering if using would for(i=0;i<varlength;i++)make any difference, like the php process for () and foreach () differently?

+3
source share
2 answers

For loops a little faster, but consider the following:

$bar = array("cow"=>"moo", "cat"=>"meaw", "dog"=>"barf");
foreach($bar as $key => $value){
 echo "The ".$key." goes ".$value.".<br>";
}
  • With foreach, you get easier access to values ​​as well as key values. This becomes easier and more obvious when using associative arrays.
  • foreach is a little easier to read and maintain.
+2
source

- , , , . foreach, , (foreach Iterator)

+2

Source: https://habr.com/ru/post/1782952/


All Articles