Tutorialink
  • Tutorials
    • C Language
    • CPP
    • Data Structure
    • JAVA
    • PHP
    • DBMS
    • JavaScript
  • About Us
  • Contact Us
  • Login / Sign up
    • Login
    • Sign-up

PHP

  1. Home
  2. PHP
  3. Nesting Loops
PHP

Nesting Loops

nested loop is a loop within a loop, an inner loop within the body of an outer one.

Syntax:

//nested for
for (expr1; expr2; expr3) {
    for (expr1; expr2; expr3) {
        //statement;
    }
}

Normally nested for loop is used. while and do.. while not used.

Example:

<?php
for ($i = 1; $i < 5; $i++) {
    for ($j = 1; $j <= $i; $j++) {
        echo " * ";
    }
    echo '<br />';
}
?>

Output:

Tutorialik.com
*
* *
* * *
* * * *

Example:

<?php
for ($i = 1; $i < 5; $i++) {
    for ($j = $i; $j < 5; $j++) {
        echo " * ";
    }
    echo '<br />';
}
?>

Output:

Tutorialik.com
* * * * 
* * * 
* * 
* 

Example:

<?php
for ($i = 1; $i < 5; $i++) {
    for ($j = $i; $j <= 5; $j++) {
        echo "&nbsp;"; // it will print blank space
    }
    for ($j = 1; $j <= $i; $j++) {
        echo " * ";
    }
    echo '<br />';
}
?>

Output:

Tutorialik.com
      * 
     * * 
    * * * 
   * * * * 


  • Prev
  • Next

Subscribe us on Youtube

Share This Page on

Question


PhpOnweb | 27-Aug-2018 05:41:13 pm

Hi dude, i have also find out one good example Loops – PHP Example


Ask Question

Introduction of PHP

  • Introduction to PHP
  • History of PHP
  • How PHP works?
  • Creating & saving a PHP file
  • Output statement

Basics of PHP

  • PHP Variable & Types
  • PHP Data Types
  • Changing & Casting Types
  • PHP Operators
  • Operator Precedence
  • Constants

Control statements

  • The if statement
  • The if else statement
  • The else if clause
  • The switch statement
  • The ? operator

Loops

  • The while statement
  • The do while statement
  • The for statement
  • The foreach statement
  • The break statement
  • The continue
  • Nesting Loops

PHP Arrays

  • Arrays
  • PHP Numeric Array
  • PHP Associative Arrays
  • PHP Multidimensional Array

PHP Functions

  • User Defined Functions
  • Variable Scope

contact info

Email : contact@tutorialink.com

Web : tutorialink.com

Tutorialink

  • About Us
  • Contact Us
  • Terms & Conditions

© Tutorialink.com - 2019