on air

While Loop | PHP Basics.

back home » Scripting Tutorials » PHP » While Loop | PHP Basics


How The While Loop Works.

The while loop, if true, its statement will execute the code, and then changes the original statement.

It will then start all over by repeating the evaluation, and continuously loop through the code until it will find a false statement to stop its looping.

Let's see how a while loop works in the script below.

<html>
<body bgcolor="#F00">
<?php
$fuel_price = 2;
$calculator = 1;
echo "<table border=\"5px\" align=\"center\" width=\"300px\">";
echo "<tr bgcolor=\"#093\"><th width=\"50%\">Fuel In Liters</th>";
echo "<th>Price Per Ltr. In US\$</th></tr>";
while ( $calculator <= 20 ) {
	echo "<tr bgcolor=\"#FFD700\"><td>";
	echo $calculator;
	echo " ltr.</td><td>$";
	echo $fuel_price * $calculator;
	echo ".00</td></tr>";
	$calculator = $calculator + 1;
  }
echo "</table>";
?>
</body>
</html>

Explaining The "While Loop" Script Above.

Please take your time and study this tutorial with a concentrative mind, because it could be confusing.

PHP Embedded In HTML.

What you should first note here is that the whole PHP script is placed inside the <body> element of an HTML document.

Variables Settings.

After the php syntax followed two variables set equal to the desired values. One for the fuel price, and the other is for the fuel calculation.

PHP Echoed <table> And Labels.

A php echoed to create a beginning <table> tag with 350px wide, 5px border, and placed it in the center of the screen.

And again echoed to create a table row <tr> with background color gold (#FFD700), and place two headers <th> with 50% size of the table on each side into it. And labeled the left side "Fuel In Liters" and the right side "Price Per Ltr. In US$" and then closed both the </tr> and the </th> tags.

[Note: The <table> isn't closed here.]

Setting Conditional Statement.

A set of WHILE LOOP's conditional statement is placed inside brackets. It says, while the $calculate is less than 20, the following code placed inside parenthesis should be executed.

Now, cheching the conditonal statement the value inside $calculate starts with 1 which is less than 20 so the conditional statement is true, and will execute the code inside the parenthesis as follows...

Code Executes When Statement Is True.

It will create a new table row <tr> with a background color gold (#FFD700), create a cell <td> and place 1 ltr. into it, close the cell </td>, and create a new cell <td>. In the new cell it will now multiply the current amount of liters of fuel to the price of fuel, and place the cost price into it.

Since the while statement is still true, because the value of liters still less than 20, 1 will be added, bringing the value to 2.

The Loop will start again, cheching to see if the value is equal to 20, if not it will execute the statement again adding 1 to the current state at everytime of execution.

While Loop Will Keep Repeating Until Statement Is False.

This process will keep repeating itself until the value is equal to 20. This will then prove the statement to be false, and stop looping. After reaching out to the desired value.

  1. The </table> tag will be closed.
  2. The php syntax ?> will be closed.
  3. The </body> tag will be closed.
  4. And at last, the </html> tag will also be closed.

Congratulations you've completed and achieved the skill of using a while loop in PHP.

PHP While Loop.

Clicks4UrMind

Man will give you worldly powers, and make you feel on top of all, note this is temporally and could be redrawn at anytime sending you back to the ground floor of life. However, the Lord Almighty, the one and the only rightful ruler of the Universe, blesses everlastingly.

Brain

Knowledge is Power, but Power is not Knowledge. Wisdom is the truthful Power that identifies the Knowledge within the man. And only, I repeat 3x. Only the almighty God has the power to bless the man with his Wisdom forever.

To achieve the Lord's blessings, cease not praying daily, asking for his Wisdom, and you'll be blessed abundantly...

SPYERA PHONE

Spyera Phone

Spyera Phone version is software that you install on a smart phone to monitor everything happening on the phone. After installation, SPYERA secretly records events (sms, call history, phone book, location, emails, What's App messages, IM, Facebook Chat, Skype, See photos taken and many more...) that happen on the phone and delivers these information to a web account, where you can view these reports 24/7 from any Internet enabled computer or mobile phone. SPYERA also allows you to listen to the surroundings of the target mobile , listen to the phone conversation and to know the location of the device.

 

Please, Kindly Donate What You Can, Enabling Us Pay Hosting And Domain Costing, And Do More.

Please Donate