How to used foreach and Difference between for and foreach in php mysql


In foreach not used third expression but in for used 3rd expression in for eaxh fetch keys and values.

in for loop firest count() and the initialization and apply condition and increment but forach is short cut

in php not set any initialization expression and not set any condition just apply and fetch keys and values 

of array 



for (initialization; condition; increment){

   code to be executed;




loop prints the value of array and fetch keys and values.


$arrayb = explode(",",$batchroomselectid);   

foreach($arrayb as $batchidkey) {

 echo $batchidkey;



2nd exaple  

  $User = array(

    'name' => 'Boy',

    'email' => '',

    'age' => 18



foreach ($User as $key => $value) {

    echo $key.' is '.$value.'<br />';



3 example


$colors = array("boy", "School", "Color", "Food");

foreach ($colors as $value) {

    echo "$value <br>";



 in case if match 23 value so skip and then next value show


$array = array( 11, 12, 23, 14, 15);        

foreach( $array as $value ) {

if( $value == 23 )continue;

echo "Value is $value <br />";