php - I want to echo all the values of the arrays using foreach loop within a function -


i trying echo values of $sel_arrays using foreach loop within function called country(). using loop within country() function , return value $sel. output single value bangladesh. want values of $sel_arrays.though know foreach loops iterates arrays values, these codes not give expected result. below codes...

    <?php      function country() {         $sel_arrays = array('banglasesh','pakistan','usa','india','italy');          foreach( $sel_arrays $sel ) {             return $sel;         }     }     echo country(); // output bangladesh. ain't wanting.     ?> 

i did not how happening. please values of array.

your function returns, , exits function, in first iteration. how return works. work around it, add values string, , echo string instead.

function country() {     $sel_arrays = array('banglasesh','pakistan','usa','india','italy');      $result = "";     foreach( $sel_arrays $sel ) {         $result .= $sel." ";     }     return $result; } echo country(); // outputs everything, separated space 

reference


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -