Sharp Tutorial
loop in php

Python

Java

C

C++

HTML/CSS

Java Script

PHP

SQL

C Programs

PHP Introduction

  • PHP home
  • PHP introduction
  • How to install and run
  • Syntax and Rule in PHP
  • Data type and operator
  • If else in php
  • Loop in php
  • Array in php
  • Sring in php
  • Function in php
  • Include in php

Working with Forms

  • Get and Post
  • Get Form input into PHP
  • Form Validation

PHP with MySQl

  • Connect PHP & Mysql
  • Insert using PHP Mysql
  • Select using PHP MySql
  • Delete Update using PHP Mysql
  • Login Page Example in PHP

Advanced PHP

  • Session in PHP
  • Cookies in PHP
  • File Handling in PHP
  • File Upload/Download PH

Object Oriented PHP

  • Class Obejct in PHP
  • Constructor in PHP
  • Destructor in PHP
  • Inheritance in PHP

loop in PHP

Loop is used to iterate a block of code mulitple times (till the given condition is true) .

There are following types of loop supported in PHP.

1)while loop

2) do while loop

3) for loop

4 for each loop

While loop syntax

while(<condition>)
{
// body part to be executed till the condtion is true
}

 

   <?php
$i=1;
while($i<=100)
{
echo "$i";
$i++;
}
?>

Above program prints the no 1-to 100 with the help of while loop.

Do While loop Syntax

//this loop executes at least once because the while condition is after do part
do

{
// body to be executed but executes at least once
}
while(<condtion>);

<?php
$i=100;
do{
echo "hello";
$i++;
}
while($i<0);
?>

Above program prints 1 time even if the condition is not true first time as well.

For loop syntax

//in for loop all three parts are in the loop the initialization,condition and increment/decrement
for(initialization; condition; increment/decrement)

{
//body part to be executed till the condition is true.
}
 
 <?php
$n=5;
for($i=1; $i<=10; $i++)
{
echo "<br>".($n*$i);
}
?>

Above program prints prints the table of the given no ie 5.

For each loop in php

   <?php
$city=array("Delhi", "Mumbai", "Chennai");
foreach($city as $c)
{
echo "$c<br>";
}
?>

For each loop works only on arrya as above program which prints the all city in the given array.

Enquiry about Course

Ask your Question

Click Here

Sharp (2) Tutorials

Video/ C Introduction

Watch video in full size

Video tutorial

Follow by Email
Facebook
Facebook
fb-share-icon
YouTube
Copyright © Sharp Tutorial
Build with WordPress
Wordpress Social Share Plugin powered by Ultimatelysocial
Sharp Tutorial