E Online Support - Free Software Download
Where does for and foreach is used in php?
I am new to php and i am not able to differenciate between them.
Shruti
Answer
1

The foreach loop is used to iterate over each element of an object—which makes it perfect for use with arrays! You can think of foreach as jumping from element to element in the array and running the code between {}s for each of those elements. Whereas for loops are great for running the same code over and over, especially when you know ahead of time how many times you'll need to loop.

<html>
  <head>
     <style>
     h1 {
    font-family: Verdana, sans-serif;
    font-size: 18px;
}

ul {
    list-style-type: decimal-leading-zero;
    font-family: Tahoma, sans-serif;
}

.wrapper {
    height: 100px;
    border-radius: 5px;
    background-color: #C3E9FA;
}
     </style>
    <title>Codecademy Languages</title>
  </head>
  <body>
    <h1>Languages you can learn on Codecademy:</h1>
    <div class="wrapper">
      <ul>
        <?php
          $langs = array("JavaScript",
          "HTML/CSS", "PHP",
          "Python", "Ruby");
        
          foreach ($langs as $lang) {
              echo "<li>$lang</li>";
          }
        
          unset($lang);
        ?>
      </ul>
    </div>
  </body>
</html>
Question*:
Description*:
Script:
Related To:
 PHP    HTML    CSS    JAVASCRIPT    JQUERY   
 AJAX    WINDOWS    GAMES    DRIVERS    MAC   
 LINUX    MOBILE    GADGETS   
Security Code:
5384
Enter Security Code:
 
Facebook   Linkedin   Skype   Twitter