Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • foreach loop in php

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 104
    Comment on it

    In php foreach loops are used to access all the elements of the array. The syntax is as

    foreach ($array as $key =>$value) 
    { 
    
    }
    

    In above syntax , the no. of times loop get executed is equal to the no of elements in the array.
    the same above syntax can be written as

    foreach ($array as $value) {
    
    }
    

    In the above code the value of the array gets assigned to the variable $value every time the loop gets executed.

    foreach loop always works on the array only with the key-value pair.

    example:

    <?php 
    $days = array("Monday", "Tuesday", "Wednesday","Thursday","Friday","Saturday", "Sunday" ); 
    
    foreach ($days as $value) {
        echo "$value <br>";
    }
    ?>
    

    output:
    Monday
    Tuesday
    Wednesday
    Thursday
    Friday
    Saturday
    Sunday

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: