Difference Between For loop and Foreach loop in PHP

Foreach Loop:-

1> Foreach loop used when you have an array, without array it’s not worked.

2> Loop working at the end of array count. i.e. an array have 4 value then loop run 4 times.

3> Syntax is :

      $array = array(“C”,”C++”,”JAVA”,”PHP”);   

      foreach($array as $language ){

         echo “Programming Language is “.$language.”.”;

      }

   Output:

     Programming Language is C.

     Programming Language is C++.

     Programming Language is JAVA.

Programming Language is PHP.

For Loop:-

1> For loop used according to condition.

    2> Loop working at the end of given condition.

    3> Syntax is :

       $array = array(“C”,”C++”,”JAVA”,”PHP”);

       for($i=0;$i<count($array);$i++){

          echo “Programming Language is “.$array[$i].”;

       }

  Output:

     Programming Language is C.

     Programming Language is C++.

     Programming Language is JAVA.

     Programming Language is PHP.

Leave a comment

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