PHP foreach Loop with Examples

Program  1

<html>
<body>
<ul>

<pre>
  <?php
  $myarray=array(10,25,'67',89,12,23);
  
  foreach($myarray as $data)
    {
         echo "<br>".$data;
  
    }

  //echo in_array(205,$myarray);
  //echo in_array(67,$myarray,true);
   //echo array_search(167,$myarray,true);
           
  // find max and minvalue from array using foreach 
  //$sum=0;
    // foreach($myarray as $data)
    // {
    //      echo "<br>".$data;
    //       $sum=$sum+$data;     
    // }
    // echo "<br>Total is= ".$sum;
      $student=array("id1"=>101,"name"=>"Amit Verma","age"=>22,"course"=>"Mtech");
                     //       key  value
                       ksort($student);
                             foreach($student as $key=> $data)
                             {
                                echo "<li>".$key."=".$data."</li>";
                              }     
?>
</pre>

    </ul>

</body>
</html>

 

courses

TechVidvan Team

TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.

Leave a Reply

Your email address will not be published. Required fields are marked *