PHP FORM Part-7


 

PHP FORM Part-7

In this part of this tutorial we are going to learn about the loop. Let's see what is loop and how can we save our tons of time by using loop? So what?s a loop then? A loop is something that moves into circle like a tier of bicycle. In programming, it?s similar to the bicycle tier. Apart from a programming loop, it will move into a circle till the time anyone tell it to stop. 

In this part of this tutorial we are going to learn about the loop. Let's see what is loop and how can we save our tons of time by using loop? So what?s a loop then? A loop is something that moves into circle like a tier of bicycle. In programming, it?s similar to the bicycle tier. Apart from a programming loop, it will move into a circle till the time anyone tell it to stop. 

Topic : HTML FORM Loop Function

Part - 7

In this part of this tutorial we are going to learn about  the loop. Let's see what is loop and how can we save our tons of time by using loop? So what?s a loop then? A loop is something that moves into circle like a tier of bicycle. In programming, it?s similar to the bicycle tier. Apart from a programming loop, it will move into a circle till the time anyone tell it to stop. 

We also need to mention two other things in our program :

1. where to start your loop, and

2. what to do after it's finished one round.

We can make program without using loops but it is not a right way to do the programming because it will consume your lot of time and space also. So, its better to use loop when we need to repeat the same code in our program.

Let's take an example :

You want to add up the numbers 1 to 4: 1 + 2 + 3 + 4. You could do it like this:

$num = 1 + 2 + 3 + 4;

echo $num;

It looks very simple to do but what happen when you need to add up a thousands of numbers in your code. I don't think so anyone would like to prefer  typing all of them. It's really terrible to do lots of typing. One code of loop would make the work lot easier to do and you can use them when you want to execute the same code over and over again.

There are few types of programming loops, but at the moment we will start from the FOR loop :

<?php

$num = 0;

for($sum=1;$sum<11;$sum++)

{

    $num = $num + 1;

    echo $num."<br/>";

}

?>

The output of the above code is: 1
2
3
4
5
6
7
8
9
10

The syntax of the for loop is : 

for (initialize the value; condition/endvalue; increment/decrement expression) {

}

Firstly, we need to mentioned the name of the loop, we are using and then in between parenthesis( ) we give the condition to our program.

Let's distribute the loop into three parts :

1. Initialize the value 

In the first condition, we tell the PHP to initialize the value of the loop. In other words, from where we want to start the loops? We initialize like this :

$sum = 1;

We?re assigning a value of 1 to a variable called $sum. Like all variables, you can make up your own name. You can set the initial condition before the loop begins, like we did:

$sum = 1

0

for($sum; $sum < 11; $sum++) {

Or you can assign your loop value right in the For Loop code:

for($sum=1; $sum < 11; $sum++) {

The result is the same ? the sum number for this loop is 1.

 

2. Condition the value 

1

The next step is to give the condition to our program or end the value. It can be anything a number, a Boolean value or a string etc.

Here, we?re telling PHP to keep going round the loop while the value of the variable $sum is Less Than 11.


for($sum; $sum < 11; $sum++) {

2


When the value of $
sum is 11 or higher, PHP will come out of the loop.

 

3. Increment and Decrement Expression

3

Every loops required a number of series to increment or decrement the value and if it couldn't update the starting value, it will stop at the starting value. In other words, we need to tell the PHP how it moves in the circle and how many rounds it will take it.


$sum++

Let's time to come out from the condition and enter into the execution part within the curly braces :

4

                                        $num = $num + 1;

                                        echo $num."<br/>";

Every time the loop goes round, the code between our two curly brackets { } gets executed:

5

Notice that we?re just incrementing the num variable by 1 each time round the loop, exactly the same as what we?re doing with the sum variable.

 

This is the end of this part of tutorial and in the next part of this tutorial, we will learn about the While loop.


 

6

Ads