How to Run While Loop With Table In PHP?

while loop with table

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

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 about the while loop.

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

Leave a Comment