Wednesday, April 10, 2024

PHP for loop

 PHP for loop

PHP for loop


Information :-

1]  The loop is the most complex loop in PHP and is used when the user knows             how often the block should be executed.

 2]  A for loop consists of an initialization statement, a test statement, and an     update statement (an increment or decrement statement).

3]  Loops
 for PHP can be used to iterate over a series of scheduled code.

4] If
 the number of iterations is known you should use it, otherwise use a loop.

5]  This means that you will use a loop when you already know how many     times you want to run a piece of code.


Syntax :-

for (initialization; condition; increment/decrement)

{

// enter code and executed 

syntax  explanation :-

  • initialization :-
  • This part is executed once before the loop starts. It often used to initialize variables.

  • condition:-
  • The condition that is evaluated before each iteration of the loop. If the condition evaluates to true, the loop continues; If it evaluates to false, the loop ends.

  • increment/decrement :- This is done after each iteration of the loop. It is usually used to increase or decrease a number.


 Flowchart     

PHP FOR loop /output


 PHP for loop

Example :-

Q 1. show all number 20 to 50.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<?php
for ($no=20;$no<50;$no++)
{
   echo $no.", ";
}
?>

</body>
</html>

 PHP for loop

Output :-


PHP  for loop output

Q.2 Show all number Between 100 to 50 descending order.

 PHP for loop

Example :-

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<?php
for($no=100; $no>50;$no--)
{
   echo$no.", ";
}
?>

</body>
</html>


 PHP for loop

Output :-

PHP program no descending order output


READ MORE

PHP Switch statement

PHP introduction

PHP variable

PHP operators

data type in php

PHP logical operators

PHPArithmetic-Operators

php Assignment Operators

php operators

php string operators

PHP While loop

No comments:

Post a Comment