Close Menu
    Facebook X (Twitter) Instagram
    Oixiesoft
    • Home
    • Services
      • WordPress Malware Removal
      • Fix WordPress Errors
      • WordPress Website Development
    • Articles
    • Contact
    Oixiesoft
    Home»Tutorial»PHP»PHP for Loop
    PHP

    PHP for Loop

    Editorial StaffBy Editorial StaffUpdated:April 30, 2022No Comments2 Mins Read
    Share
    Facebook Twitter LinkedIn Pinterest Email

    PHP Loops

    Loops in PHP are used to run the same part of code a specified number of times.

    PHP for Loop

    The for loop executes a block of code again and again for a specified number of times.

    Syntax

    for (init; condition; increment)
     {
      code to be executed;
     }
    

    PHP for loop Parameters:

    • init: It’s set a counter value.
    • condition: Evaluated for each loop iteration. If it considers TRUE, the loop continues. If it considers FALSE, the for loop ends.
    • increment: Mostly used to increment a counter value.

    Example

    for ($x = 0; $x <= 30; $x+=5) {
      echo "The number is: $x <br>";
    }
    

    Output

    The number is: 0
    The number is: 5
    The number is: 10
    The number is: 15
    The number is: 20
    The number is: 25
    The number is: 30

    The foreach Loop

    The PHP foreach loop is used to loop through arrays.

    Syntax

    foreach ($array as $value)
     {
      code to be executed;
     }
    

    Example

    $fruits = array("apple", "kiwi", "mango", "banana");
    
    foreach ($fruits as $value) {
      echo "$value <br>";
    }
    

    Output

    apple
    kiwi
    mango
    banana

    Example

    $hbhero = array(
        "name" => "John Parker",
        "email" => "[email protected]",
        "age" => 28
    );
     
    // Loop through superhero array
    foreach($hbhero as $key => $value){
        echo $key . " : " . $value . "<br>";
    }
    

    Output

    name : John Parker
    email : [email protected]
    age : 28
    Share. Facebook Twitter Pinterest LinkedIn WhatsApp Reddit Tumblr Email
    Editorial Staff

    Related Posts

    CodeIgniter vs Laravel: A Detailed Side-by-Side Comparison

    Python Arrays

    Python Lambda

    PHP
    • PHP Comments
    • PHP if else Statements
    • The PHP switch Statement
    • PHP Functions
    • PHP for Loop
    • PHP Arrays
    • PHP Include Files
    • PHP Date() and Time() Function
    • PHP Forms Tutorial
    Services
    • Web Development
    • Mobile Application Development
    • WordPress Malware Removal Service
    • Website Design
    • WordPress Development
    • Magento Development
    • Shopify Development
    • SEO Services
    Blog
    • How to Fix the Error Establishing a Database Connection
    • Ping List WordPress
    • How To Fix Japanese Keyword Hack
    • How to remove Malware from WordPress
    Hire Developers
    • Hire WordPress Developer
    • Hire Shopify Developer
    Contact Info
    • Oixiesoft Technologies
      A-40, Block A, I thum Tower, Sector 62, Noida
    • [email protected]
    • Privacy Policy
    • About Us
    • Contact Us
    © 2025 OixieSoft Technologies

    Type above and press Enter to search. Press Esc to cancel.