Home PHP How to Run While Loop With Table In PHP

How to Run While Loop With Table In PHP

How to Run While Loop With Table In PHP

While loops are a type of loop that repeats continuously until some condition becomes false. The While example checks to see if the user has entered less than five numbers into the input field, and will only then stop repeating itself when this happens; otherwise, it keeps going indefinitely.

Loop With Table in PHP is a very simple and easy-to-use loop that allows for the output to be stored locally as you type.

The table will auto expand when needed, making sure all of your data gets saved correctly!

While loop with table

Often whenever you write the code, you want the matching block of code to run overwrite some name of students. So, alternatively of adding several almost equal code lines in a PHP, we can use the while loop.

Run While Loop With Table In PHP

The while loop is used to execute the same block of code again and again, as long as some condition is true.

In this article, we will discuss while loop in PHP and as well as run while loop with table in PHP.

First of all, we want to tell you about the while loop what is the while loop.

The while loop is used to repeat a section of code an unknown Name of the student until a specific condition is met.

  • while – loops through a block of code as long as the specified condition is true

For example, I want to know how many students are in my center. is William name available in my students name, so now go ahead and see.

How to write while loops code?

First, we set a variable before the loop starts (var i = 0;) Then, we define the condition for the while loop run. As long as the variable is less than the length of the array (name), the loop will continue. Each time the loop carries out of the variable is incremented by one (i++).

See the image It will be the final answers

The PHP code see below;

$student_names = ["Liam", "Noah", "Oliver", "Mai", "William", "Olivia", "Emma", "Ava", "Sophia", "Elijah", "Isabella"];

$i = 0;
$find_student = "William";
if($student_names[$i] == $find_student)
echo "Student Found";

if($i >= count($student_names))
echo "Student not Found";

see the image and write the code properly like it.

Now you have seen the PHP code. then the second thing is how to see the condition is ok or wrong.

I have an idea I should use the HTML table tag I think this way is good now go to your sublime text and open that file which file you made to run the while loop.

  • Opened now the table is ready below see it from attention.
  • According to the official website of PHP explained and shared the example.
<table border="1">
<?php for($i = 1; $i <= count($student_names); $i++) { ?>
<td><?php echo $i; ?></td>
<td><?php echo $student_names[$i-1] ?></td>
<?php } ?>

see the image and write the code properly like it.

Run While Loop With Table In PHP

Read more: Array_Merge VS Array_Combine in PHP


Please enter your comment!
Please enter your name here