myphpfile
myphpfile
1. Take values from the user and compute sum, subtraction, multiplication,
division and exponent of value of the variables.
<?php
$a = (float)readline("Enter the first number: ");
$b = (float)readline("Enter the second number: ");
Page | 1
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005
<?php
function areaOfShapes() {
echo "1. Circle\n2. Rectangle\n3. Triangle\n4. Square\n5. Trapezoid\n6. Parallelogram\n";
$choice = readline("Enter the shape number to calculate its area: ");
switch ($choice) {
case 1:
$radius = (float)readline("Enter the radius: ");
echo "Area of Circle: " . (pi() * $radius * $radius) . PHP_EOL;
break;
case 2:
$length = (float)readline("Enter the length: ");
$width = (float)readline("Enter the width: ");
echo "Area of Rectangle: " . ($length * $width) . PHP_EOL;
break;
case 3:
$base = (float)readline("Enter the base: ");
$height = (float)readline("Enter the height: ");
echo "Area of Triangle: " . (0.5 * $base * $height) . PHP_EOL;
break;
case 4:
$side = (float)readline("Enter the side length: ");
echo "Area of Square: " . ($side * $side) . PHP_EOL;
break;
case 5:
$a = (float)readline("Enter the first base: ");
$b = (float)readline("Enter the second base: ");
$height = (float)readline("Enter the height: ");
echo "Area of Trapezoid: " . (0.5 * ($a + $b) * $height) . PHP_EOL;
break;
case 6:
Page | 2
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005
Page | 3
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005
<?php
$a = (float)readline("Enter the coefficient a: ");
$b = (float)readline("Enter the coefficient b: ");
$c = (float)readline("Enter the coefficient c: ");
$discriminant = $b ** 2 - 4 * $a * $c;
if ($discriminant < 0) {
echo "No real roots, discriminant is negative." . PHP_EOL;
} elseif ($discriminant == 0) {
$root = -$b / (2 * $a);
echo "Single root: $root" . PHP_EOL;
} else {
$root1 = (-$b + sqrt($discriminant)) / (2 * $a);
$root2 = (-$b - sqrt($discriminant)) / (2 * $a);
echo "Roots are $root1 and $root2" . PHP_EOL;
}
?>
Output:-
Page | 4
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005
<?php
$a = (float)readline("Enter side a: ");
$b = (float)readline("Enter side b: ");
$c = (float)readline("Enter side c: ");
if ($a == $b || $b == $c || $a == $c) {
echo "The triangle is isosceles." . PHP_EOL;
} else {
echo "The triangle is not isosceles." . PHP_EOL;
}
?>
Output:-
Page | 5
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005
<?php
$num = (int)readline("Enter a number: ");
for ($i = 1; $i <= 10; $i++) {
echo "$num x $i = " . ($num * $i) . PHP_EOL;
}
?>
Output:-
Page | 6
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005
<?php
$n = (int)readline("Enter a number: ");
$sum = ($n * ($n + 1)) / 2;
echo "Sum of natural numbers up to $n is $sum" . PHP_EOL;
?>
Output:-
Page | 7
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005
<?php
$n = (int)readline("Enter the number of terms: ");
$f1 = 0;
$f2 = 1;
Page | 8
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005
<?php
$num = (int)readline("Enter a number: ");
$factorial = 1;
Page | 9
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005
<?php
$start = (int)readline("Enter the start of the range: ");
$end = (int)readline("Enter the end of the range: ");
Page | 10
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005
10. Write a program to compute, the Average and Grade of students marks.
<?php
$marks = [];
$numSubjects = (int)readline("Enter the number of subjects: ");
Page | 11
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005
<?php
echo "Enter the elements of the first matrix (2x2):\n";
$matrix1 = [
[(int)readline("Matrix1[0][0]: "), (int)readline("Matrix1[0][1]: ")],
[(int)readline("Matrix1[1][0]: "), (int)readline("Matrix1[1][1]: ")]
];
// Addition
echo "Addition:\n";
for ($i = 0; $i < 2; $i++) {
for ($j = 0; $j < 2; $j++) {
echo $matrix1[$i][$j] + $matrix2[$i][$j] . " ";
}
echo "\n";
}
?>
Output:-
Page | 12
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005
Page | 13
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005
12. Count total number of vowels in a word “Develop & Empower Individuals”.
<?php
$string = "Develop & Empower Individuals";
$vowelCount = preg_match_all('/[aeiouAEIOU]/', $string);
echo "Input: \"$string\"\n";
echo "Total vowels: $vowelCount\n";
?>
Output:-
Page | 14
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005
<?php
$string = readline("Enter a string: ");
$reversed = strrev($string);
echo "Input: $string\n";
if ($string === $reversed) {
echo "The string is a palindrome.\n";
} else {
echo "The string is not a palindrome.\n";
}
?>
Output:-
Page | 15
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005
<?php
$string = readline("Enter a sentence: ");
$words = explode(" ", $string);
sort($words);
echo "Input: $string\n";
echo "Sorted: " . implode(" ", $words) . "\n";
?>
Output:-
Page | 16
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005
<?php
$num = (int)readline("Enter a number: ");
$start = (int)readline("Enter the start of the range: ");
$end = (int)readline("Enter the end of the range: ");
echo "Input: $num, Range: [$start, $end]\n";
if ($num >= $start && $num <= $end) {
echo "The number is in the range.\n";
} else {
echo "The number is not in the range.\n";
}
?>
Output:-
Page | 17
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005
16. Write a program accepts a string and calculates number of upper case letters and lower
case letters available in that string.
<?php
$string = readline("Enter a string: ");
$uppercase = preg_match_all('/[A-Z]/', $string);
$lowercase = preg_match_all('/[a-z]/', $string);
echo "Input: $string\n";
echo "Uppercase: $uppercase\n";
echo "Lowercase: $lowercase\n";
?>
Output:-
Page | 18
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005
<?php
$string = readline("Enter a sentence: ");
$words = explode(" ", $string);
$reversed = implode(" ", array_reverse($words));
echo "Input: $string\n";
echo "Reversed: $reversed\n";
?>
Output:-
Page | 19
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005
18. Write a program to create a login form. On submitting the form, the user should navigate
to profile page.
Page | 20
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Arial, sans-serif; background-color: #f0f0f0; text-align: center; }
.header { background-color: #4CAF50; color: white; padding: 10px; }
.content { margin: 20px; }
</style>
</head>
<body>
<div class="header">
<h1>Welcome to XYZ College</h1>
</div>
<div class="content">
<p>Empowering the Future</p>
</div>
</body>
</html>
Output:-
Page | 21
Programming in PHP Laboratory UGCA-1930 Roll no:- 2202005
Page | 22