Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

Ex No 1 Control Structures in PHP

The document discusses different control structures in PHP including if/else statements, switch statements, while loops, do-while loops, and for loops. Code examples are provided to demonstrate the syntax and usage of each structure. Procedures to explain how each example works are also included.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Ex No 1 Control Structures in PHP

The document discusses different control structures in PHP including if/else statements, switch statements, while loops, do-while loops, and for loops. Code examples are provided to demonstrate the syntax and usage of each structure. Procedures to explain how each example works are also included.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Ex no 1 Control Structures in PHP

1a, If else statement

<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:

1. Create an HTML Document:

 Begin by creating a new HTML file using a text editor.


 Add the basic structure of an HTML document:

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.

1b, Switch Statement


<html>
<head>
<title>Switch Demo</title>
</head>
<body>
<?php
$x=15;
$y=10;
$op='*';
switch($op)
{
case '+': $z = $x + $y;
echo "Addition is : $z";
break;
case '-': $z = $x - $y;
echo "Subtraction is : $z";
break;
case '*': $z = $x * $y;
echo "Multiplication is : $z";
break;
case '/': $z = $x / $y;
echo "Division is : $z";
break;
case '%': $z = $x % $y;
echo "Modulus is : $z";
break;
default: echo "Invalid Operator";
}
?>
</body>
</html>

Procedure
1. Start
2. Declare variables:

x as a number to hold the first operand

y as a number to hold the second operand

op as a character to hold the operator

3. Assign x to 15, y to 10 and op to '*' (initially set to multiplication)


4. Use a switch statement to evaluate the operator:
o Case '+' (addition) , Calculate z = x + y and Display "Addition is : $z", Break out of
the switch statement
o Case '-' (subtraction),Calculate z = x – y ,Display "Subtraction is : $z" and Break
out of the switch statement
o Case '*' (multiplication), Calculate z = x * y, Display "Multiplication is : $z" and
Break out of the switch statement
o Case '/' (division), Calculate z = x / y And Display "Division is : $z",Break out of the
switch statement
o Case '%' (modulus), Calculate z = x % y, Display "Modulus is : $z" and Break out of
the switch statement
o Default (invalid operator), Display "Invalid Operator"
5. End

1C, while loop

<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.

1d, do - while loop

<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.

Inside the Loop:

 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.

1e, for loop

<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:

 The for loop begins with these actions:


o Variable Initialization: A variable named $i is created and set to the value 1.
o Condition Check: The loop checks if $i is less than or equal to 5.

5. Execute Loop Body (if condition is true):

 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.

You might also like