0% found this document useful (0 votes)
133 views15 pages

PHP 1

The document outlines a course on Web Application Development using PHP, detailing its objectives, prerequisites, and various programming assignments. Students will learn PHP basics, database operations with MySQL, AJAX, and validations through practical exercises. The document includes multiple PHP programming tasks aimed at reinforcing the concepts taught in the course.

Uploaded by

jimoma785
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)
133 views15 pages

PHP 1

The document outlines a course on Web Application Development using PHP, detailing its objectives, prerequisites, and various programming assignments. Students will learn PHP basics, database operations with MySQL, AJAX, and validations through practical exercises. The document includes multiple PHP programming tasks aimed at reinforcing the concepts taught in the course.

Uploaded by

jimoma785
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/ 15

Course Name: Web Application Development using PHP

Course Code: DSC-M-BCA-354 P

Credits: 4
Course Outcomes:

The aim of this course is to enable students to

 Get familiar with server-side language for web development.


 Learn basic concepts of PHP, functions for database operations using MySQL.
 Learn about AJAX in PHP for faster processing of web applications.
 Get familiar with validations.

Prerequisites:

 Basic knowledge of data types, HTML tags.

Unit-1 1. Write a PHP program to check whether the number entered


Basic Programs is Positive / Negative or Zero entered by user.
2. Write a PHP program to find maximum number from the two
numbers entered by user
3. Write a PHP program that prints Fibonacci series till the
number entered by user.
4. Accept a string from user. Write PHP program to check
whether that entered string is palindrome or not.
5. Write a PHP program to accept a number from user and find
its square and cube.
6. Write a PHP program to find sum of n numbers. Take input
from user.
7. Accept a two numbers from user. Write a PHP program to
Add, Subtract and Multiply the numbers and displays result.
8. Accept a number from user as “Radius” of a circle. Write PHP
program find Area of a Circle. ( Area = PI * radius * radius).
9. Write a PHP program to print all even numbers till the
entered number.
10. Create a function that accepts 3 numbers as parameters and
check whether the sum of 3 numbers is prime or not. Note:
Input should be collected from user using an HTML page.
11. Create a function that accepts a number as argument and
displays sum of digit of a number. E.g. If Number is 123 then
sum of Digits is 6 i.e., 1+2+3 =6 Note: Input should be
collected from user using an HTML page.
12. Write a PHP program that changes color of the web page
using switch case.

PROF.DEVRAJSINH JADAV 1
1. Write a PHP program to check whether the number entered is Positive / Negative or Zero entered
by user.

Input:
<!DOCTYPE html>
<html>
<head>
<title>Check Number</title>
</head>
<body>
<h2>Check if Number is Positive, Negative, or Zero</h2>
<form method="post">
Enter a number: <input type="number" name="number" required>
<input type="submit" name="check" value="Check">
</form>

<?php
if (isset($_POST['check'])) {
$number = $_POST['number'];

if ($number > 0) {
echo "<p>The number $number is <strong>Positive</strong>.</p>";
} elseif ($number < 0) {
echo "<p>The number $number is <strong>Negative</strong>.</p>";
} else {
echo "<p>The number is <strong>Zero</strong>.</p>";
}
}
?>

</body>
</html>

Output:

PROF.DEVRAJSINH JADAV 2
2. Write a PHP program to find maximum number from the two numbers entered by user .

Input:
<!DOCTYPE html>
<html>
<head>
<title>Find Maximum</title>
</head>
<body>
<h2>Find the Maximum of Two Numbers</h2>
<form method="post">
Enter first number: <input type="number" name="num1" required><br><br>
Enter second number: <input type="number" name="num2" required><br><br>
<input type="submit" name="compare" value="Find Maximum">
</form>
<?php
if (isset($_POST['compare'])) {
$a = $_POST['num1'];
$b = $_POST['num2'];

if ($a > $b) {


echo "<p>The maximum number is: $a</p>";
} elseif ($b > $a) {
echo "<p>The maximum number is: $b</p>";
} else {
echo "<p>Both numbers are equal: $a</p>";
}
}
?>
</body>
</html>
Output:

PROF.DEVRAJSINH JADAV 3
3. Write a PHP program that prints Fibonacci series till the number entered by user.

Input:
<!DOCTYPE html>
<html>
<head>
<title>Fibonacci Series</title>
</head>
<body>

<h2>Print Fibonacci Series up to a Number</h2>

<form method="post">
Enter a number: <input type="number" name="limit" required>
<input type="submit" name="generate" value="Generate">
</form>

<?php
if (isset($_POST['generate'])) {
$limit = $_POST['limit'];

$a = 0;
$b = 1;

echo "<p>Fibonacci series up to $limit:</p>";


echo "<p>";

// Print Fibonacci numbers until limit


while ($a <= $limit) {
echo $a . " ";
$next = $a + $b;
$a = $b;
$b = $next;
}

echo "</p>";
}
?>

</body>
</html>

PROF.DEVRAJSINH JADAV 4
Output:

4. Accept a string from user. Write PHP program to check whether that entered string is palindrome
or not.

Input:
<!DOCTYPE html>
<html>
<head>
<title>Palindrome Checker</title>
</head>
<body>

<h2>Check if a String is Palindrome</h2>

<form method="post">
Enter a string: <input type="text" name="text" required>
<input type="submit" name="check" value="Check">
</form>

<?php
if (isset($_POST['check'])) {
$input = $_POST['text'];
$reversed = strrev($input);

if (strtolower($input) == strtolower($reversed)) {
echo "<p>'$input' is a <strong>Palindrome</strong>.</p>";
} else {
echo "<p>'$input' is <strong>Not a Palindrome</strong>.</p>";
}
}
?>
</body>
</html>

PROF.DEVRAJSINH JADAV 5
Output:

5. Write a PHP program to accept a number from user and find its square and cube.

Input:
<!DOCTYPE html>
<html>
<head>
<title>Square and Cube</title>
</head>
<body>

<h2>Find Square and Cube of a Number</h2>

<form method="post">
Enter a number: <input type="number" name="num" required>
<input type="submit" name="calculate" value="Calculate">
</form>

<?php
if (isset($_POST['calculate'])) {
$n = $_POST['num'];
$square = $n * $n;
$cube = $n * $n * $n;

echo "<p>Number: $n</p>";


echo "<p>Square: $square</p>";
echo "<p>Cube: $cube</p>";
}

?>

PROF.DEVRAJSINH JADAV 6
</body>
</html>
Output:

6. Write a PHP program to find sum of n numbers. Take input from user.

Input:
<!DOCTYPE html>
<html>
<head>
<title>Sum of N Numbers</title>
</head>
<body>

<h2>Find Sum of First N Numbers</h2>

<form method="post">
Enter a number (n): <input type="number" name="n" required>
<input type="submit" name="calculate" value="Find Sum">
</form>

<?php
if (isset($_POST['calculate'])) {
$n = $_POST['n'];
$sum = 0;

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


$sum += $i;
}

echo "<p>The sum of first $n natural numbers is: <strong>$sum</strong></p>";


}

PROF.DEVRAJSINH JADAV 7
?>

</body>
</html>

Output:

7. Accept a two numbers from user. Write a PHP program to Add, Subtract and Multiply the numbers
and displays result.

Input:
<!DOCTYPE html>
<html>
<head>
<title>Arithmetic Operations</title>
</head>
<body>

<h2>Enter Two Numbers to Perform Operations</h2>

<form method="post">
Enter first number: <input type="number" name="num1" required><br><br>
Enter second number: <input type="number" name="num2" required><br><br>
<input type="submit" name="calculate" value="Calculate">
</form>

<?php
if (isset($_POST['calculate'])) {
$a = $_POST['num1'];
$b = $_POST['num2'];

PROF.DEVRAJSINH JADAV 8
$sum = $a + $b;
$difference = $a - $b;
$product = $a * $b;

echo "<h3>Results:</h3>";
echo "Addition: $a + $b = $sum<br>";
echo "Subtraction: $a - $b = $difference<br>";
echo "Multiplication: $a × $b = $product<br>";
}
?>

</body>
</html>

Output:

8. Accept a number from user as “Radius” of a circle. Write PHP program find Area of a Circle.
( Area = PI * radius * radius).

Input:
<!DOCTYPE html>
<html>
<head>
<title>Area of Circle</title>
</head>
<body>

<h2>Calculate Area of a Circle</h2>

<form method="post">
Enter Radius: <input type="number" name="radius" step="0.01" required>
<input type="submit" name="calculate" value="Find Area">
</form>

PROF.DEVRAJSINH JADAV 9
<?php
if (isset($_POST['calculate'])) {
$radius = $_POST['radius'];
$pi = 3.14159;
$area = $pi * $radius * $radius;

echo "<p>Radius: $radius</p>";


echo "<p>Area of the circle: <strong>$area</strong></p>";
}
?>

</body>
</html>
Output:

9. Write a PHP program to print all even numbers till the entered number.

Input:
<!DOCTYPE html>
<html>
<head>
<title>Even Numbers</title>
</head>
<body>

<h2>Print All Even Numbers Up to a Given Number</h2>

<form method="post">
Enter a number: <input type="number" name="num" required>
<input type="submit" name="print" value="Show Even Numbers">
</form>

1
PROF.DEVRAJSINH JADAV
0
<?php
if (isset($_POST['print'])) {
$limit = $_POST['num'];

echo "<p>Even numbers from 1 to $limit:</p>";


echo "<p>";
for ($i = 2; $i <= $limit; $i += 2) {
echo $i . " ";
}
echo "</p>";
}
?>

</body>
</html>

Output:

10. Create a function that accepts 3 numbers as parameters and check whether the sum of 3 numbers
is prime or not. Note: Input should be collected from user using an HTML page.

Input:
<!DOCTYPE html>
<html>
<head>
<title>Check Prime Sum</title>
</head>
<body>
<h2>Enter 3 Numbers to Check if Their Sum is Prime</h2>
<form method="post">
Number 1: <input type="number" name="num1" required><br><br>
Number 2: <input type="number" name="num2" required><br><br>

1
PROF.DEVRAJSINH JADAV
1
Number 3: <input type="number" name="num3" required><br><br>
<input type="submit" name="check" value="Check Sum">
</form>

<?php
// Function to check if a number is prime
function isPrime($n) {
if ($n < 2) return false;
for ($i = 2; $i <= sqrt($n); $i++) {
if ($n % $i == 0) return false;
}
return true;
}
if (isset($_POST['check'])) {
$a = $_POST['num1'];
$b = $_POST['num2'];
$c = $_POST['num3'];

$sum = $a + $b + $c;
echo "<p>Sum of $a + $b + $c = $sum</p>";
if (isPrime($sum)) {
echo "<p><strong>$sum is a Prime number.</strong></p>";
} else {
echo "<p><strong>$sum is NOT a Prime number.</strong></p>";
}
}
?>

</body>
</html>
Output:

1
PROF.DEVRAJSINH JADAV
2
11. Create a function that accepts a number as argument and displays sum of digit of a number. E.g.
If Number is 123 then sum of Digits is 6 i.e., 1+2+3 =6 Note: Input should be collected from user using
an HTML page.

Input:
<!DOCTYPE html>
<html>
<head>
<title>Sum of Digits</title>
</head>
<body>

<h2>Enter a Number to Find Sum of Its Digits</h2>

<form method="post">
Enter Number: <input type="number" name="num" required>
<input type="submit" name="calculate" value="Find Sum">
</form>

<?php
// Function to calculate sum of digits
function sumOfDigits($number) {
$sum = 0;
while ($number > 0) {
$digit = $number % 10;
$sum += $digit;
$number = (int)($number / 10);
}
return $sum;
}

if (isset($_POST['calculate'])) {
$num = abs($_POST['num']); // Use abs() to handle negative numbers
$sum = sumOfDigits($num);

echo "<p>Sum of digits of <strong>$num</strong> is: <strong>$sum</strong></p>";


}
?>

</body>
</html>

1
PROF.DEVRAJSINH JADAV
3
Output:

12. Write a PHP program that changes color of the web page using switch case.

Input:
<!DOCTYPE html>
<html>
<head>
<title>Change Page Color</title>
</head>
<body>
<form method="post">
<h2>Select a Color to Change Background</h2>
<select name="color" required>
<option value="">--Choose Color--</option>
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
<option value="yellow">Yellow</option>
<option value="pink">Pink</option>
</select>
<input type="submit" name="change" value="Apply Color">
</form>
<?php
$bgColor = "white"; // Default color
if (isset($_POST['change'])) {
$selectedColor = $_POST['color'];
// Use switch to assign color
switch ($selectedColor) {
case "red":
$bgColor = "red";

1
PROF.DEVRAJSINH JADAV
4
break;
case "green":
$bgColor = "green";
break;
case "blue":
$bgColor = "blue";
break;
case "yellow":
$bgColor = "yellow";
break;
case "pink":
$bgColor = "pink";
break;
default:
$bgColor = "white";
break;
}
}
?>
<!-- Apply background color dynamically -->
<style>
body {
background-color: <?= $bgColor ?>;
}
</style>
</body>
</html>
Output:

***BEST OF LUCK***
1
PROF.DEVRAJSINH JADAV
5

You might also like