PHP Loops

Posted on Updated on

PHP Loops

Frequently when you compose code, you need the same piece of code to keep running again and again in succession. Rather than including a few verging on equivalent code-lines in a script, we can utilize loops to perform an assignment like this.

In PHP, we have the accompanying circling articulations:

While – loops through a piece of code the length of the predetermined condition is valid.

Do…While – loops through a square of code once, and afterward rehashes the loop the length of the predetermined condition is valid.

For – loops through a square of code a predefined number of times.

ForEach – loops through a square of code for every component in a cluster.

PHP While Loop

The while loop executes a square of code the length of the predetermined condition is valid.

Language Structure

while ( condition ) {

code to be executed ;

}

The sample beneath first sets a variable $ x to 1 ( $ x = 1 ) . At that point, the while loop will keep on

running the length of $ x is not exactly, or equivalent to 5 ( $ x < = 5 ) . $ x will increment by 1 every

Time the Loop Runs ( $ x++ ) :

Illustration

< ? php

$ x = 1 ;

while ( $ x < = 5 ) {

echo “ The number is: $ x < br> “ ;

$ x++ ;

}

?>

The PHP do…while Loop

The do…while loop will dependably execute the piece of code once, it will then check the condition, and rehash the loop while the predefined condition is valid.

Language structure

do {

code to be executed ;

} while ( condition is valid ) ;

The illustration underneath first sets a variable $ x to 1 ( $ x = 1 ) . At that point, the do while loop will compose some yield, and afterward increase the variable $ x with 1. At that point the condition is checked ( is $ x not exactly, or equivalent to 5? ) , and the loop will keep on running the length of $ x is not exactly, or equivalent to 5:

< ? php

$ x = 1 ;

do {

echo “ The number is: $ x < br> “ ;

$ x++ ;

} while ( $ x < = 5 ) ;

?>

Notice that in a do while loop the condition is tried AFTER executing the announcements inside of the loop. This implies the do while loop would execute its announcements in any event once, regardless of the possibility that the condition is false the first run through.

The sample underneath sets the $ x variable to 6, then it runs the loop, and afterward the condition is checked:

Sample

< ? php

$ x = 6 ;

Do

{

echo “ The number is: $ x < br> “ ;

$ x++ ;

}

while ( $ x < = 5 ) ;

?>

For more details and queries please feel free to email, visit or call us. Wishing you the very best for all your future endeavors.

Helpline: 9814666333, 8699444666

Email:info@technocampus.co.in

**************************************************************************************************************

Please fill the FORM and we shall contact you soon.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s