PHP Programs WT
PHP Programs WT
Algorithm:
1. Start
2. Initialize three variables: a, b, and c with values (e.g., a=10, b=20, c=30).
3. Check if a > b:
4. If true, check if a > c:
a. If true, print "a is the largest number."
b. Otherwise, print "c is the largest number."
5. Else (if a <= b), check if b > c:
1. If true, print "b is the largest number."
2. Otherwise, print "c is the largest number."
6. End
Program:
<html>
<head>
<title>Selection Demo</title>
</head>
<body>
<?php
print '<h2>Selection Statement</h2>';
$a = 10;
$b = 20;
$c = 30;
Output:
Algorithm:
1. Start
2. Initialize a variable sum to 0.
3. Loop through numbers from 1 to 10:
4. Add the current number to sum.
5. Calculate the average:
6. Divide sum by 10 and store the result in avg.
7. Print the sum and average.
8. End
Program:
<html>
<head>
<title>Sum and Average</title>
</head>
<body>
<center>
<?php
$sum = 0;
for ($i = 1; $i <= 10; $i++) {
$sum += $i;
}
Output:
3)The PHP program to print whether the current year is a leap year or not:
Algorithm:
1. Start
2. Initialize a variable year with a value (e.g., 2016).
3. Check if year is divisible by 4:
4. If true, print "Year is a leap year."
5. Otherwise, print "Year is not a leap year."
6. End
Program:
<html>
<head>
<title>Leap Year Demo</title>
</head>
<body>
<?php
$year = 2016;
print "<br/>";
if ($year % 4 == 0) {
printf("Year %d is a leap year", $year);
} else {
printf("Year %d is not a leap year", $year);
}
?>
</body>
</html>
Output:
if ($num % 2 == 1) {
printf("Number %d is Odd", $num);
} else {
printf("Number %d is Even", $num);
}
print "<br/>";
}
?>
</body>
</html>
Output:
5)The PHP script to compute the factorial of 'n' using a for loop:
Algorithm:
1. Start
2. Initialize a variable n with the number (e.g., 5).
3. Set factorial = 1 to hold the product.
4. Loop from i = n down to 1:
5. Multiply factorial by i.
6. Print the value of factorial as the factorial of n.
7. End
Program:
<html>
<head>
<title>Factorial Program</title>
</head>
<body>
<?php
$n = 5;
$factorial = 1;
Output:
6)The PHP script to compute the square root, square, cube, and quad of 10 numbers:
Algorithm:
1. Start
2. Print the table header with columns: "num", "Sqr", "Cube", and "Quad".
3. Loop through numbers from 1 to 10:
4. For each count:
1. Calculate the square: sq = count * count.
2. Calculate the cube: cube = count * count * count.
3. Calculate the quadruple: quad = count * count * count * count.
5. Print a table row displaying count, sq, cube, and quad.
6. End
Program:
<html>
<head>
<title>SQUARE CUBE QUAD DEMO</title>
</head>
<body>
<?php
print "<b>Table</b><br/>";
print "<table border='1'>";
print "<tr><th>num</th><th>Sqr</th><th>Cube</th><th>Quad</th></tr>";
print "<tr><td>$count</td><td>$sq</td><td>$cube</td><td>$quad</td></tr>";
}
print "</table>";
?>
</body>
</html>
Output:
7)The PHP Program To Print A Message Based On Today's Date Using Switch And If
Structures
Algorithm:
1. Start
2. Get today's date (day) using date("d").
3. Check if the day is less than 3 or greater than 10:
If true, print "No Event!!!".
4. Otherwise, check if the day is between 4 and 9:
If true, print "No Event!!!".
5. Else (the day is either 3 or 10):
If day is 3, print "Dentist Appointment".
If day is 10, print “Go to Conference”.
6. End
Program:
<!DOCTYPE html>
<html>
<body>
<?php
echo “Today date is “ . date(“d/m/y”) . “<br>”;
Output:
8)The Php Code To Display The Pattern
1
01
101
0101
10101
Algorithm:
1. Start
2. Loop from i = 1 to 5 (for each row):
3. Loop from j = 1 to i (for each column in that row):
a) Check if the sum of i + j is even:
i) If true, print 0.
ii) Otherwise, print 1.
4. After completing the inner loop, print a newline.
5. End
Program:
<html>
<head>
<title>Pattern Demo</title>
</head>
<body>
<?php
for ($i = 1; $i <= 5; $i++) {
for ($j = 1; $j <= $i; $j++) {
if (($i + $j) % 2 == 0) {
printf("0");
} else {
printf("1");
}
}
print "<br/>";
}
?>
</body>
</html>
Output: