PHP File
PHP File
<html>
<title>Code one user input</title>
<body><center><h1>Arithmatic operation using User input in php
<br>*****************************************</h1></center>
<form action="code_one.php" method="get">
<p>Enter 1st Number: <input type="number" name='a' value="<?php echo $a;
?>"></p>
<p>Enter 2nd Number: <input type="number" name='b' value="<?php echo $b;
?>"></p>
<button type="submit" name="submit">Submit</button>
</form>
<?php
if(isset($_GET['submit'])){
$a=$_GET['a'];
$b=$_GET['b'];
$c=$a+$b;
echo "Sum of $a and $b is ".$c;
$d=$a-$b;
echo "<br>Subtraction of $a and $b is ".$d;
$e=$a*$b;
echo "<br>Multiplication of $a and $b is ".$e;
$f=$a/$b;
echo "<br>Division of $a and $b is ".$f;
$g=$a%$b;
echo "<br>Modulas of $a and $b is ".$g;
$h=$a**2;
echo "<br>$a Power 2 is ".$h;
} ?>
</body> </html>
Program 1. Take values from the user and compute Sum, Subtraction,
Multiplication, Division and Exponent of value of the variables.
OUTPUT PROGRAM 1
**********
Program 2. Write a program to find area of following shapes: circle,
rectangle, triangle, square, trapezoid and parallelogram.
( a ) Following the code of PHP to find area of circle, rectangle, triangle, square,
trapezoid and parallelogram.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Area of shapes</title>
</head>
<body>
<center>
<h1>AREA OF SHAPES IN PHP <br>*********************************</h1>
</center>
<?php
$r=5;
$pi=3.14;
$a=$pi*$r*$r;
echo " code (a) AREA OF CIRCLE is = ", $a;
?>
<br/>
<?php
$length = 14;
$width = 12;
echo " code (b) AREA OF RECTANGLE IS = $length * $width = " . ($length * $width);
?>
<br/>
<?php
$base = 10;
$height = 15;
echo"code (c) AREA OF TRIANGLE";
echo "area with base $base and height $height = " . ($base * $height) / 2;
?>
<br/>
<?php
$s=13;
$area_square = $s*$s;
echo "code (d) AREA OF SQUARE =";
echo $area_square;
?>
<br/>
<?php
$h1=0;
$b1=32; $b2=12;
$a1 = ($b1+ $b2)/2*$h1;
echo sprintf("code (e) AREA OF TRAPEZOID : %f ",$a1);
?>
<br/>
<?php
$base1=4;
$height2=18;
$area_parallelogram=$base1*$height2;
echo " code (f) Area of the parallelogram=";
echo $area_parallelogram;
?>
<br/>
</body>
</html>
Program 2. Write a program to find area of following shapes: circle, rectangle,
triangle, square, trapezoid and parallelogram.
OUTPUT PROGRAM 2
**********
Program 3. Compute and print roots of quadratic equation.
<html>
<head>
<title> PHP Program To find the roots of Quadratic Equation</title>
</head>
<body>
<center>
<h1>PHP Program To find the roots of Quadratic Equation
<br>*****************************************************</h1>
<form method="post">
<table border="0">
<tr>
<td> <input type="text" name="num1" value="" placeholder="Enter 1st value"/> </td>
</tr>
<tr>
<td> <input type="text" name="num2" value="" placeholder="Enter 2nd value"/> </td>
</tr>
<tr>
<td> <input type="text" name="num3" value="" placeholder="Enter 3rd value"/> </td>
</tr>
<tr>
<td> <input type="submit" name="submit" value="Submit"/>
</td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit']))
{
$a = $_POST['num1'];
$b = $_POST['num2'];
$c = $_POST['num3'];
$precision = 4;
$d = $b * $b - 4 * $a * $c; // Discriminant of a quadratic equation
if($d < 0)
{
echo " Roots are complex numbers. " ;
echo " Roots of quadratic equation are: " ;
echo -$b/(2*$a). " + " .number_format(sqrt(-$d)/(2*$a), $precision)."i ";
echo ", ". -$b/(2*$a).number_format(-sqrt(-$d)/(2*$a), $precision)."i" ;
}
else if($d == 0)
{
echo "Both roots are equal.";
$root1 = -$b /(2* $a);
echo"Root of quadratic equation is: " .$root1 ;
return 0;
}
else
{
echo "Roots are real numbers.";
$root1 = ( -$b + sqrt($d)) / (2* $a );
$root2 = ( -$b - sqrt($d)) / (2* $a);
echo "Roots of quadratic equation are: " .$root1 .$root2 ;
}
return 0;
}
?>
</center>
</body>
</html>
Program 3. Compute and print roots of quadratic equation.
OUTPUT PROGRAM 3
**********
Program 4. Write a program to determine whether a triangle is
isosceles or not?
<html>
<head>
<title>PHP Program To read three sides of a triangle and print the type of the
triangle</title>
</head>
<body>
<center>
<h1>Program to determine a triangle is isosceles or
not.<br>********************************************</h1>
<form method="post">
<table border="0">
<tr>
<td> <input type="text" name="num1" value="" placeholder="Enter length of 1st
side"/>
</td>
</tr>
<tr>
<td> <input type="text" name="num2" value="" placeholder="Enter length of 2nd
side"/>
</td>
</tr>
<tr>
<td> <input type="text" name="num3" value="" placeholder="Enter length of 3rd
side"/>
</td>
</tr>
<tr>
<td> <input type="submit" name="submit" value="Submit"/>
</td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit']))
{
$a = $_POST['num1'];
$b = $_POST['num2'];
$c = $_POST['num3'];
echo "Given sides : ".$a." ".$b." ".$c. "</br>";
if(($a*$a)+($b*$b)==($c*$c) || ($b*$b)+($c*$c)==($a*$a) || ($c*$c)+($a*$a)==($b*$b)){
echo("Right angle triangle");}
else
if(($a==$b) && ($b==$c))
{
echo("Equilateral triangle");
}
else
if(($a==$b) || ($b==$c) || ($c==$a))
{
echo("Isosceles triangle");
}
else
if(($a!=$b && $b!=$c && $c!=$a))
{
echo("Scalene triangle");
}
return 0;
}
?>
</center>
</body>
</html>
Program 4. Write a program to determine whether a triangle is isosceles or not?
OUTPUT PROGRAM 4
**********
Program 5. Print multiplication table of a number input by the user.
User input.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Multiplication table of a number input by the user</title>
</head>
<body><center>
<h1>Multiplication table of a number input by the use
<br>*******************************************</h1>
<form Method="post"><br><br>
Enter number:<input type="text" name="value"/><br><br>
<input type="submit" name="btn" value="print table" />
</form>
<?php
if(isset($_POST['btn'])){
$n=$_POST['value'];
$res=0;
for($i=1;$i<=10;$i++){
$res=$n*$i;
echo $n;
echo"*";
echo $i;
echo "=";
echo $res;
echo "<br>";}}
?>
</center> </body>
</html>
Program 5. Print multiplication table of a number input by the user.
OUTPUT PROGRAM 5
**********
Program 6. Calculate sum of natural numbers from one to n number.
( a ) Following the code of php to. calculate the sum of natural numbers from
one to n number.
<html>
<head>
<title>PHP Program To find the Sum of N natural numbers</title>
</head>
<body>
<center>
<h1>PHP Program To find the Sum of N natural numbers
<br>*********************************</h1>
<form method="post">
<table border="0"><tr>
<td> <input type="text" name ="num" value="" placeholder="Enter a positive
integer"/> </td></tr><tr>
<td> <input type="submit" name ="submit" value="Submit"/> </td></tr>
</table>
</form>
<?php
if(isset($_POST['submit'])){
$n = $_POST['num'];
$sum = 0;
//Using loop to do sum of N natural numbers
for($i = 1; $i <= $n; ++$i){
$sum = $sum + $i;}
echo "Sum of $n natural numbers = $sum";
return 0;
}
?>
</center>
</body>
</html>
Program 6. Calculate sum of natural numbers from one to n number.
OUTPUT PROGRAM 6
**********
Program 7. Print Fibonacci series up to n numbers e.g. 0 1 1 2 3 5 8
13 21…..n.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fibonacci Series Program in PHP using For Loop</title>
</head>
<body>
<center>
<H1>Fibonacci Series Program for n number's
<br>*****************************************</H1>
<?php
$f1 = 0;
$f2 = 1;
$n = 30;
echo $f1;
echo '<br/>';
echo $f2;
for($i = 1; $i <= $n; $i++) {
$f3 = $f1 + $f2;
$f1 = $f2;
$f2 = $f3;
echo $f3 ."<br />";
}
?>
</center>
</body>
</html>
Program 7. Print Fibonacci series up to n numbers e.g. 0 1 1 2 3 5 8 13 21…..n.
OUTPUT PROGRAM 7
**********
Program 8. Write a program to find the factorial of any number.
<html>
<head>
<title> Factorial Program</title>
</head>
<body>
<center>
<h1> Factorial Program <br>**************************</h1>
<form method="POST">
<label>Enter a number</label>
<input type="text" name="number" />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
if ($_POST['submit'] == "Submit") {
$input = $_POST['number'];
$fact = 1;
//iterating using for loop
for ($i = $input; $i >= 1; $i--) {
$fact = $fact * $i;
}
// Print output of the program
echo '<br>' . 'The factorial of the number ' . $input . ' is ' . $fact;
}
?>
</center>
</body>
</html>
Program 8. Write a program to find the factorial of any number.
OUTPUT PROGRAM 8
**********
Program 9. Determine prime numbers within a specific range.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>To check if the number is prime or not</title>
</head>
<body>
<center>
<h1>Check if the number is prime or not <br>
**********************************</h1>
<form method="post">
Enter a Number: <input type="text" name="input"><br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
if($_POST){
$input=$_POST['input'];
for ($i = 2; $i <= $input-1; $i++) {
if ($input % $i == 0) {
$value= True;}}
if (isset($value) && $value) {
echo '<br>'.'The Number '. $input . ' is not prime';
} else {
echo '<br>'.'The Number '. $input . ' is prime';}
}?>
</center>
</body>
</html>
Program 9. Determine prime numbers within a specific range.
OUTPUT PROGRAM 9
**********
Program 10. Write a program to compute, the Average and Grade of
students marks.
( a ) Following the code of php to compute, the Average and Grade of students
marks
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Student Grades</title>
</head>
<body>
<center>
<h1>Program to declare the result of
student<br>****************************************</h1>
<form method="post">
<table border="0">
<tr>
<td> <input type="text" name="marks1" value="" placeholder="Enter marks of subject
1"/>
</td>
<tr>
<td> <input type="text" name="marks2" value="" placeholder="Enter marks of subject
2"/>
</td>
<tr>
<td> <input type="text" name="marks3" value="" placeholder="Enter marks of subject
3"/>
</td>
</tr>
<tr>
<td> <input type="text" name="marks4" value="" placeholder="Enter marks of subject
4"/>
</td>
</tr>
<tr>
<td> <input type="text" name="marks5" value="" placeholder="Enter marks of subject
5"/>
</td>
</tr>
<tr>
<td> <input type="submit" name="submit" value="Submit"/>
</td>
</tr>
</table>
</form>
<?php
if(isset($_POST['submit'])){
$sub_1 = $_POST['marks1'];
$sub_2 = $_POST['marks2'];
$sub_3 = $_POST['marks3'];
$sub_4 = $_POST['marks4'];
$sub_5 = $_POST['marks5'];
}
$total = NULL;
$average = NULL;
$percentage = NULL;
$grade = NULL;
// calculation
$total = $sub_1 + $sub_2 + $sub_3 + $sub_4 + $sub_5;
$average = $total / 5.0;
$percentage = ($total / 500) * 100;
if ($average >= 90)
$grade = "O";
else if ($average >= 80 && $average < 90)
$grade = "A+";
else if ($average >= 70 && $average < 80)
$grade = "B+";
else if ($average >= 60 && $average < 70)
$grade = "B";
else
$grade = "C";
// to print output
echo '<br>'."The Total marks = " . $total . "/500\n";
echo '<br>'."The Average marks = " . $average . "\n";
echo '<br>'."The Percentage = " . $percentage . "%\n";
echo '<br>'."The Grade = '" . $grade . "'\n";
?>
</center>
</body>
</html>
Program 10. Write a program to compute, the Average and Grade of students
marks.
OUTPUT PROGRAM 10
**********
Program 11. Compute addition, subtraction and multiplication of a
matrix.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Compute addition, subtraction and multiplication of a matrix</title>
</head>
<body>
<center>
<h1>Compute addition, subtraction and multiplication of a
matrix<br>*********************************</h1>
<?php
$a = array(array(1,2,3),array(4,5,6),array(7,8,9));
$b = array(array(7,8,9),array(4,5,6),array(1,2,3));
$m=count($a);
$n=count($a[2]);
$p=count($b);
$q=count($b[2]);
echo "the first matrix :"."<br/>";
for ($row = 0; $row < $m; $row++) {
for ($col = 0; $col < $n; $col++)
echo " ".$a[$row][$col];
echo "<br/>";
}
echo "the second matrix :"."<br/>";
for ($row = 0; $row < $p; $row++) {
for ($col = 0; $col < $q; $col++)
echo " ".$b[$row][$col];
echo "<br/>";
}
echo "the transpose for the first matrix is:"."<br/>";
for ($row = 0; $row < $m; $row++) {
for ($col = 0; $col < $n; $col++)
echo " ".$a[$col][$row];
echo "<br/>";
}
echo "the addition of matrices is:"."<br/>";
for ($row = 0; $row < 3; $row++) {
for ($col = 0; $col < 3; $col++)
echo " ".$a[$row][$col]+$b[$row][$col]." ";
echo "<br/>";
}
echo " The multiplication of matrices: <br/>";
$result=array();
for ($i=0; $i < $m; $i++) {
for($j=0; $j < $q; $j++){
$result[$i][$j] = 0;
for($k=0; $k < $n; $k++)
$result[$i][$j] += $a[$i][$k] * $b[$k][$j];
}
}
for ($row = 0; $row < $m; $row++) {
for ($col = 0; $col < $q; $col++){
echo " ".$result[$row][$col];
}
echo "<br/>";
}
?>
</center>
</body>
</html>
Program 11. Compute addition, subtraction and multiplication of a matrix.
OUTPUT PROGRAM 11
**********
Program 12. Count total number of vowels in a word “Develop &
Empower Individuals”.
( a ) Following the code of php to count the total number of vowels in a word
“Develop & Empower Individuals”.
<!DOCTYPE html>
<html>
<body>
<center >
<h3>Total number of vowels in a word “Develop & Empower
Individuals”<br>**************************************************</h3>
<?php
$vCount = 0;
$str = "Develop and Empower Individuals";
$str = strtolower($str);
OUTPUT PROGRAM 12
**********
Program 13. Determine whether a string is palindrome or not?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>String is Palidrome or not</title>
</head>
<body>
<center>
<h1>Palidrome String <br>**********************************</h1>
<form action="#" method="post">
<label for="#">Enter a string</label>
<input type="text" name="str" size="70"><br><br>
<input type="submit" name=" " value="Check">
</form>
<?php
$sub=isset($_POST['str']);
if($sub){
$rev=strrev($_POST['str']);
if($rev==$_POST['str']){
echo $_POST['str']." String is palidrome";
}
else
echo $_POST['str']." String is not palidrome";
}
?>
</center>
</body>
</html>
Program 13. Determine whether a string is palindrome or not?
OUTPUT PROGRAM 13
**********
Program 14. Display word after Sorting in alphabetical order.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Alphabet orders</title>
</head>
<body>
<center>
<h1>Sorting in alphabetical order. <br>***********************************</h1>
<h3>
<?php
$word = " DCABFEGH ";
$word = str_split($word);
sort($word);
$word = implode($word);
echo "DCABFEGH is sorted to <br><br><br>";
echo $word;
?>
</h3>
</center>
</body>
</html>
Program 14. Display word after Sorting in alphabetical order.
OUTPUT PROGRAM 14
**********
Program 15. Check whether a number is in a given range using
functions.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>number is in a given range using functions.</title>
</head>
<body>
<center>
<h1>Check if number is in a given range using functions
<br>*************************************</h1>
<h3>Given range is = 10 to 100 </h3><h4>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="#">Enter input</label>
<input type="text" name="int">
<input type="submit" value="Submit">
</form>
<?php
$int = $_POST['int'];
$min = 10;
$max = 100;
if ($int > $min && $int < $max) {
echo "$int is in range";} else {
echo "$int is not range";}
?>
</center> </h4>
</body>
</html>
Program 15. Check whether a number is in a given range using functions.
OUTPUT PROGRAM 15
**********
Program 16. Write a program accepts a string and calculates number
of upper case letters and lower case letters available in that string.
( a ) Following the code to calculates number of upper case letters and lower
case letters available in a string
<!DOCTYPE html>
<head>
OUTPUT PROGRAM 16
Fig.16.0 ( Output number of upper case letters and lower case letters)
**********
Program 17. Design a program to reverse a string word by word.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Reverse a string word by word.</title>
</head>
<body>
<center>
<h1>A program to reverse a string word by word.
<br>*************************************</h1>
<h2>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="#">Enter input</label>
<input type="text" name="str">
<input type="submit" value="Submit">
</form> <br>
<?php
$str = $_POST['str'];
$arr = explode(" ", $str);
$arr = array_reverse($arr);
$str = implode(" ", $arr);
echo $str;
?>
</h2>
</center>
</body>
</html>
Program 17. Design a program to reverse a string word by word.
OUTPUT PROGRAM 17
**********
Program 18. Write a program to create a login form. On submitting
the form, the user should navigate to profile page.
<!DOCTYPE html>
<head>
<title>login form in php</title>
</head>
<body>
<center>
<h1>A program of a login form.<br>*************************************</h1>
<h2>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<label for="username">Username:</label>
<input type="text" name="username" id="username">
<br>
<label for="password">Password:</label>
<input type="password" name="password" id="password">
<br>
<input type="submit" value="Login">
</form>
<?php
if (isset($_POST['username']) && isset($_POST['password'])) {
$username = $_POST['username'];
$password = $_POST['password'];
if ($username == 'jaspreet' && $password == 'admin') {
echo "<br>Welcome to profile page $username";
} else {
echo "<br>Invalid username or password";}}
?>
</h2>
</center>
</body> </html>
Program 18. Write a program to create a login form. On submitting the form,
the user should navigate to profile page.
OUTPUT PROGRAM 18
**********
Program 19. Design front page of a college or department using
graphics method.
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<link rel="stylesheet" href="styles.css">
</head>
<style>
/********** CSS **********/
*{
margin:0px;
padding:0px;
}
html, body {
font-family: 'Times New Roman', Times, serif;
font-size: 14px;
color: #242424;
}
.container {
width: 1200px;
margin:0px auto;
}
.main-section {
width: 100%;
float: left;
padding:50px 0px 40px 0px;
}
.heading {
font-size: 22px;
font-weight: 500;
border-bottom: 1px solid #ff5722;
margin-bottom:25px;
color: #ff5722;
}
/********** CSS Ends **********/
/********** Logo CSS Starts **********/
.logo {
width:25%;
}
/********** Logo CSS Ends **********/
**********
Program 20. Write a program to upload and download files.
<?php
if (isset($_POST["submit"])) {
if (isset($_FILES["image"])) {
$targetDir = "uploads";
$targetFile = $targetDir . basename($_FILES["image"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($targetFile, PATHINFO_EXTENSION));
$check = getimagesize($_FILES["image"]["tmp_name"]);
if ($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;}
}
if (file_exists($targetFile)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
if ($_FILES["image"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
if (
$imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif"
){
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;}
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
} else {
if (move_uploaded_file($_FILES["image"]["tmp_name"], $targetFile)) {
echo "The file " . htmlspecialchars(basename($_FILES["image"]["name"])) . " has been
uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";}}}
?>
<!DOCTYPE html>
<html>
<head>
<title>Upload and display image in PHP</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<link href="form.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="phppot-container">
<h1>Upload an Image <br>**********************</h1>
<form action="" method="post" enctype="multipart/form-data">
<div class="row">
<input type="file" name="image" required>
<input type="submit" name="submit" value="Upload">
</div>
</form>
<h1>Display uploaded Image:</h1>
<?php if (isset($_FILES["image"]) && $uploadOk == 1) : ?>
<img src="<?php echo $targetFile; ?>" alt="Uploaded Image">
<?php endif; ?>
<p> Download the image:
<a href="<?php echo $target_file; ?>">Download</a>
</p>
</div>
</body>
</html>
Program 20. Write a program to upload and download files.
OUTPUT PROGRAM 20
**********