Ex No 1 Control Structures in PHP
Ex No 1 Control Structures in PHP
<html>
<head>
<title>Conditional Demo</title>
</head>
<body>
<?php
$x=15;
$y=5;
if ($x > $y)
{
echo "$x is greater than $y";
}
else if ($x < $y)
{
echo "$x is less than $y";
}
else
{
echo "Both are Equal";
}
?>
</body>
</html>
Procedure:
2. Within the <body> section, embed the PHP code using the opening and
closing tags <?php and ?>:
3. Declare two variables, $x and $y, and assigning them values of 15 and
5, respectively.
4 if ($x > $y): This condition checks if $x is greater than $y. If it's
true, the code within the first block executes, printing "$x is greater than
$y".
5 else if ($x < $y): If the first condition is false, this condition checks
if $x is less than $y. If true, the code within the second block
runs, printing "$x is less than $y".
6 else: If both of the previous conditions are false, this block
executes, indicating that the values are equal, and printing "Both are
Equal".
7 The echo statements within each conditional block are responsible for
displaying the appropriate message based on the comparison
of $x and $y.
8 Save the file with a .php extension
9 Open the file in a web browser to execute the PHP code and see the
output.
Procedure
1. Start
2. Declare variables:
<html>
<head>
<title>While Demo</title>
</head>
<body>
<h1>While Demo</h1>
<?php
$n=1;
while($n<=5)
{
echo "$n <br/>";
$n++;
}
?>
</body>
</html>
PROCEDURE
Explanation:
1. The algorithm starts by setting the variable n to 1, which will be used as a counter.
2. It then enters a while loop that continues as long as n is less than or equal to 5.
3. Inside the loop, the current value of n is displayed, followed by a line break.(br tag)
4. The value of n is then incremented by 1.
5. The loop repeats steps 3 and 4 until n becomes greater than 5, at which point the
loop terminates.
<html>
<head>
<title>Do-While Demo</title>
</head>
<body>
<h1>Do-While Demo</h1>
<?php
$n=1;
do
{
echo "$n <br/>";
$n++;
} while($n<=5);
?>
</body>
</html>
Procedure
Here's a breakdown of the algorithm in the given PHP code:
1. Initialization:
The variable $n is created and assigned the value 1. This variable acts as a
counter.
2. Do-While Loop:
The do-while loop ensures that the code within it executes at least once. It
continues to execute as long as the specified condition remains true.
Printing: The current value of $n is printed to the screen, followed by a line break
using <br/>. This displays each number on a separate line.
Incrementing: The value of $n is increased by 1 using $n++. This prepares the
counter for the next iteration.
3. Condition Check:
The loop condition $n <= 5 is evaluated. If $n is still less than or equal to 5, the loop
continues. Otherwise, it terminates.
4. Loop Termination:
The loop repeats steps 2 and 3 until the condition $n <= 5 becomes false. This
happens when $n reaches 6.
<html>
<head>
<title>For Demo</title>
</head>
<body>
<h1>For Demo</h1>
<?php
for($i=1;$i<=5;$i++)
{
echo "$i <br/>";
}
?>
</body>
</html>
Procedure
1. The browser receives the HTML code and begins rendering the page.
2. The browser encounters the <h1> tag and displays "For Demo" as a level 1 heading.
3. The browser reaches the <?php tag, signaling the start of PHP code execution.
4. Initialize Loop:
Since $i is initially 1 (which is less than or equal to 5), the loop body executes:
o Output Value: The current value of $i is echoed to the browser, followed by a
line break (<br/>).
o Increment Counter: The value of $i is incremented by 1.
6. The loop returns to step 4, checking the condition again. This process repeats as long
as $i is still less than or equal to 5.
7.When $i reaches 6, the condition becomes false, and the loop ends.
8. The browser encounters the ?> tag, signaling the end of PHP code execution.