0% found this document useful (0 votes)
19 views26 pages

Rahul PHP

PHP simple project
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views26 pages

Rahul PHP

PHP simple project
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

INDEX

Sr No Date Remark Sing


Assignment Name
Write a PHP program to swap two numbers with and
1
without using third variable

2 Write a PHP program to find the factorial of a number.

Write a PHP program to count the total number of


3
words in a string.

Write a program in PHP to find the occurrence of a


4
word in a string.

5 Write a PHP program to replace a word in a string.

Write a PHP program to demonstrate various functions


6
of regular expression.

Write a PHP program to find area of triangle and


7
rectangle using functions.

Write a PHP program to find the GCD of two numbers


8
using user defined functions.

Write a Program for finding the biggest number in an


9
array without using any array Functions.

Write a Program for finding the smallest number in an


10
array.

11 Write a PHP program to design a simple calculator.

Design a simple web page to generate multiplication


12
table for a given number using PHP.

Design a web page that should compute one’s age on a


13
given date using PHP.

Write a PHP program read Student information


(Roll_No, Name, Class, Contact_No, email_id) using
14
HTML form and display this information using
GET/POST method.

Write a PHP program to read student marks for


15
semester subjects
Krantiagrani Dr. G.D. Bapu Lad Mahavidyalaya, Kundal
B.C.A Department
Year 2023-2024
Roll No: - Name: -Rahul Vijay Khurasane
Assignment No: - Date: - Class: - BCA-II
Subject: PHP SEM-IV

Q1.) Write a PHP program to swap two numbers with and without
using third variable

//swaping with using third variable

<?php

$a = 45;

$b = 78;

$third = $a;

$a = $b;

$b = $third;

echo "After swapping:<br><br>";

echo "a =".$a." b=".$b;

?>

<br><br>

//without using third variable

<?php

$a=234;
$b=345;

$a=$a+$b;

$b=$a-$b;

$a=$a-$b;

echo "Value of a: $a</br>";

echo "Value of b: $b</br>";

?>

O/P:-
Q2.) Write a PHP program to find the factorial of a number

<?php

$num = 14597;

$sum=0; $rem=0;

for ($i =0; $i<=strlen($num);$i++)

$rem=$num%10;

$sum = $sum + $rem;

$num=$num/10;

echo "Sum of digits 14597 is $sum";

?>

O/P: -
Q3.) Write a PHP program to count the total number of words
in a string.

<?php

$str = " BCA Department ";

$len = str_word_count($str);

echo $len;

?>

O/P: -
Q4.) Write a program in PHP to find the occurrence of a word in a
string.

<?php

// PHP program to count number of times

// a s2 appears in s1.

$s1 = " Hello ";

$s2 = " BCA ";

$res = substr_count($s1,$s2);

echo($res);

?>

O/P: -
Q5.) Write a PHP program to replace a word in a string.

<?php

// PHP program to replace all occurrence

// of a word inside a string

// Given string

$str = "Rahul Khurasane";

$w1 = "khurasane";

$w2 = "khurasane";

$str = str_replace($w1, $w2, $str);

echo $str;

?>

O/P: -
Q6.) Write a PHP program to demonstrate various functions of
regular expression.

<html>

<body>

<?php

$str3 = "Hello Microsoft!";

$pattern3 = "/microsoft/i";

echo preg_replace($pattern3, "Rahul Khurasane", $str3);

?>

</body>

</html>

O/P :-
Q7.) Write a PHP program to find area of triangle and rectangle
using functions.

<?php

// Function to calculate the area of a triangle

function calculateTriangleArea($base, $height) {

return 0.5 * $base * $height;

// Function to calculate the area of a rectangle

function calculateRectangleArea($length, $width) {

return $length * $width;

// Example values for triangle and rectangle

$triangleBase = 5;

$triangleHeight = 8;

$rectangleLength = 6;

$rectangleWidth = 10;

// Calculate areas

$triangleArea = calculateTriangleArea($triangleBase, $triangleHeight);

$rectangleArea = calculateRectangleArea($rectangleLength,
$rectangleWidth);

// Output the results


echo "Area of Triangle: " . $triangleArea . "\n";

echo "Area of Rectangle: " . $rectangleArea . "\n";

?>

O/P: -
Q8.) Write a PHP program to find the GCD of two numbers using
user defined function.

<?php

function gcd($x, $y) {

if ($y == 0)

return $x;

return gcd($y, $x%$y);

$x = 250;

$y = 475;

echo "GCD of $x and $y is: ".gcd($x,$y);

?>

O/P:-
Q9.) Write a Program for finding the biggest number in an array
without using any array Functions.

<?php

$a = array(10, 30, 89, 64, 23);

$lar = $a[0];

$s = count($a);

for ($i = 0; $i < $s; $i++) {

if ($a[$i] > $lar)

$lar = $a[$i];

echo "The biggest number in the array is: $lar";

?>

O/P:-
Q10. Write a Program for finding the smallest number in an array.

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Document</title>

</head>

<body>

<?php

$numbers= array(170, 210, 103, 375, 315, 470, 255);

echo(min($numbers));

?>

</body>

</html>

O/P:-
Q11.) Write a PHP program to design a simple calculator.

<!DOCTYPE html>

<head>

<title>Simple Calculator Program in PHP - Tutorials Class</title>

</head>

<?php

$first_num = $_POST['first_num'];

$second_num = $_POST['second_num'];

$operator = $_POST['operator'];

$result = '';

if (is_numeric($first_num) && is_numeric($second_num)) {

switch ($operator) {

case "Add":

$result = $first_num + $second_num;

break;

case "Subtract":

$result = $first_num - $second_num;

break;

case "Multiply":

$result = $first_num * $second_num;

break;

case "Divide":
$result = $first_num / $second_num;

?>

<body>

<div id="page-wrap">

<h1>PHP - Simple Calculator Program</h1>

<form action="" method="post" id="quiz-form">

<p>

<input type="number" name="first_num" id="first_num"


required="required" value="<?php echo $first_num; ?>" /> <b>First
Number</b>

</p>

<p>

<input type="number" name="second_num" id="second_num"


required="required" value="<?php echo $second_num; ?>" /> <b>Second
Number</b>

</p>

<p>

<input readonly="readonly" name="result" value="<?php echo


$result; ?>"> <b>Result</b>

</p>

<input type="submit" name="operator" value="Add" />


<input type="submit" name="operator" value="Subtract" />

<input type="submit" name="operator" value="Multiply" />

<input type="submit" name="operator" value="Divide" />

</form>

</div>

</body>

</html>

O/P: -
Q12. Design a simple web page to generate multiplication table for a given
number using PHP.

<!DOCTYPE html>

<html>

<body>

<center>

<h3>

Program to print multiplication<br>

table of any number in PHP

</h3>

<form method="POST">

Enter a number:

<input type="text" name="number">

<input type="Submit"

value="Get Multiplication Table">

</form>

</center>

</body>

</html>

<?php

if($_POST) {

$num = $_POST["number"];

echo nl2br("<p style='text-align: center;'>

Multiplication Table of $num: </p>

");

for ($i = 1; $i <= 10; $i++) {


echo ("<p style='text-align: center;'>$num"

. " X " . "$i" . " = "

. $num * $i . "</p>

");

?>

O/P:-
Q13. Design a web page that should compute one’s age on a given date using
PHP.

<!DOCTYPE html>

<html>

<head>

<title>Age Calculator</title>

</head>

<body>

<h2>Age Calculator</h2>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<label for="birthdate">Enter your birthdate:</label>

<input type="date" id="birthdate" name="birthdate" required><br><br>

<label for="targetdate">Enter the target date:</label>

<input type="date" id="targetdate" name="targetdate"


required><br><br>

<input type="submit" name="calculate" value="Calculate Age">

</form>

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {

// Get birthdate and target date from the form


$birthdate = $_POST["birthdate"];

$targetdate = $_POST["targetdate"];

// Calculate age

$diff = date_diff(date_create($birthdate), date_create($targetdate));

$age = $diff->format('%y');

// Display age

echo "<h3>Your age on $targetdate:</h3>";

echo "<p>$age years</p>";

?>

</body>

</html>

O/P: -
Q14. Write a PHP program read Student information (Roll_No, Name, Class,
Contact_No, email_id) using HTML form and display this information using
GET/POST method.

<!DOCTYPE html>

<html>

<head>

<title>Student Information Form</title>

</head>

<body>

<h2>Student Information Form</h2>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<label for="rollno">Roll Number:</label>

<input type="text" id="rollno" name="rollno" required><br><br>

<label for="name">Name:</label>

<input type="text" id="name" name="name" required><br><br>

<label for="class">Class:</label>

<input type="text" id="class" name="class" required><br><br>

<label for="contact">Contact Number:</label>

<input type="text" id="contact" name="contact" required><br><br>


<label for="email">Email ID:</label>

<input type="email" id="email" name="email" required><br><br>

<input type="submit" name="submit" value="Submit">

</form>

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$rollno = $_POST["rollno"];

$name = $_POST["name"];

$class = $_POST["class"];

$contact = $_POST["contact"];

$email = $_POST["email"];

echo "<h2>Student Information</h2>";

echo "<p><strong>Roll Number:</strong> $rollno</p>";

echo "<p><strong>Name:</strong> $name</p>";

echo "<p><strong>Class:</strong> $class</p>";

echo "<p><strong>Contact Number:</strong> $contact</p>";

echo "<p><strong>Email ID:</strong> $email</p>";

?>

</body>

</html>
O/p:-
Q15. Write a PHP program to read student marks for semester
subjects with other required details (prn, name, rollno, class ….etc.)
and display semester mark list.
<!DOCTYPE html>

<html>

<head>

<title>Semester Mark List</title>

</head>

<body>

<h2>Enter Student Details and Marks</h2>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

<label for="prn">PRN:</label>

<input type="text" id="prn" name="prn" required><br><br>

<label for="name">Name:</label>

<input type="text" id="name" name="name" required><br><br>

<label for="rollno">Roll Number:</label>

<input type="text" id="rollno" name="rollno" required><br><br>

<label for="class">Class:</label>

<input type="text" id="class" name="class" required><br><br>

<h3>Enter Marks for Semester Subjects</h3>

<label for="subject1">Subject 1:</label>

<input type="number" id="subject1" name="subject1" required><br><br>

<label for="subject2">Subject 2:</label>

<input type="number" id="subject2" name="subject2" required><br><br>


<label for="subject3">Subject 3:</label>

<input type="number" id="subject3" name="subject3" required><br><br>

<label for="subject4">Subject 4:</label>

<input type="number" id="subject4" name="subject4" required><br><br>

<input type="submit" name="submit" value="Generate Mark List">

</form>

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$prn = $_POST["prn"];

$name = $_POST["name"];

$rollno = $_POST["rollno"];

$class = $_POST["class"];

$subject1 = $_POST["subject1"];

$subject2 = $_POST["subject2"];

$subject3 = $_POST["subject3"];

$subject4 = $_POST["subject4"];

// Calculate total marks and percentage

$total_marks = $subject1 + $subject2 + $subject3 + $subject4;

$percentage = ($total_marks / 400) * 100;

// Display mark list

echo "<h2>Semester Mark List</h2>";

echo "<p><strong>PRN:</strong> $prn</p>";

echo "<p><strong>Name:</strong> $name</p>";

echo "<p><strong>Roll Number:</strong> $rollno</p>";

echo "<p><strong>Class:</strong> $class</p>";


echo "<h3>Marks Obtained:</h3>";

echo "<p><strong>Subject 1:</strong> $subject1</p>";

echo "<p><strong>Subject 2:</strong> $subject2</p>";

echo "<p><strong>Subject 3:</strong> $subject3</p>";

echo "<p><strong>Subject 4:</strong> $subject4</p>";

echo "<p><strong>Total Marks:</strong> $total_marks / 400</p>";

echo "<p><strong>Percentage:</strong> $percentage%</p>";

?>

</body>

</html>

O/P: -

You might also like