0% found this document useful (0 votes)
8 views30 pages

1 2

Uploaded by

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

1 2

Uploaded by

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

1. Develop a program to find given number is even or odd.

Ans:

<html>
<body>
<form action="even_odd.php" method="post">
Enter the number : <input type="number" name="t1"><br>
<input type="submit" name="btn" value="Submit">
</form>
<?php
if(isset($_POST["btn"]))
{
$no=$_POST["t1"];
if($no%2==0)
{
echo " Number is even ";
}
else
{
echo "Number is odd ";
}
}
?>
</body>
</html>

2. Develop a program to find given year is leap year or not.

Ans:

<html>
<body>
<form action="leap.php" method="post">
Enter the Year : <input type="number" name="t1"><br>
<input type="submit" name="btn" value="Submit">
</form>
<?php
if(isset($_POST["btn"]))
{
$yr=$_POST["t1"];
if($yr%4==0) // or you can use if(($yr % 4 == 0 && $yr % 100 != 0) || ($yr %
400 == 0))
{
echo " It is leap year ";
}
else
{
echo "It is not leap year ";
}
}
?>
</body>
</html>

3. Develop a program to find largest number from 3 numbers.

Ans:

<html>
<body>
<form action="large.php" method="post">
Enter the no1 : <input type="number" name="t1"><br>
Enter the no2 : <input type="number" name="t2"><br>
Enter the no3 : <input type="number" name="t3"><br>
<input type="submit" name="btn" value="Submit">
</form>
<?php
if(isset($_POST["btn"]))
{
$a=$_POST["t1"];
$b=$_POST["t2"];
$c=$_POST["t3"];
if($a>$b && $a>$c)
{
echo $a." is greater";
}
if($b>$a && $b>$c)
{
echo $b." is greater";
}
if($c>$a && $c>$b)
{
echo $c." is greter";
}
}
?>
</body>
</html>

4. Develop a program to find average of 3 subjects and calculate the class which has

Ans:

<html>
<body>
<form action="per.php" method="post">
Enter the marks of sub1 : <input type="number" name="t1"><br>
Enter the marks of sub2 :<input type="number" name="t2"><br>
Enter the marks of sub3 :<input type="number" name="t3"><br>
<input type="submit" name="btn" value="Submit">
</form>
<?php
if(isset($_POST["btn"]))
{
$sub1=$_POST["t1"];
$sub2=$_POST["t2"];
$sub3=$_POST["t3"];
$sum=$sub1+$sub2+$sub3;
$avg=$sum/3;
if($avg>=75 && $avg<100){
echo "Passed with Distinction";
}
else if($avg>60 && $avg<75){
echo "Passed with First Class";
}
else if($avg>=40 && $avg<60){
echo "Passed with Second Class";
}
else if($avg<40){
echo "Failed";
}
}
?>
</body>
</html>

5. Write a program to convert number into word (ex. 2 – two)

Ans:

<html>
<body>
<form action="no_to_word.php" method="post">
Enter the no : <input type="number" name="t1"><br>
<input type="submit" name="btn" value="Submit">
</form>
<?php
if(isset($_POST["btn"]))
{
$ch=$_POST["t1"];
switch($ch){
case 0 :
echo "Zero";
break;
case 1 :
echo "One";
break;
case 2 :
echo "Two";
break;
case 3 :
echo "Three";
break;
case 4 :
echo "Four";
break;
case 5 :
echo "Five";
break;
case 6 :
echo "Six";
break;
case 7 :
echo "Seven";
break;
case 8 :
echo "Eight";
break;
case 9 :
echo "Nine";
break;
default :
echo "Invalid no";
}
}
?>
</body>
</html>

6. Develop a program to find sum of first 12 numbers.

Ans:

<?php
$sum=0;
for($i=1;$i<=12;$i++)
{
$sum=$sum+$i;
}
echo "Sum of first 12 numbers : ".$sum;
?>

7. Write a program to print fibonacci series of first 10 numbers.

Ans:
<?php
$first = 0;
$second = 1;

echo "$first, $second";

for($i = 3; $i <= 10; $i++)


{
$next = $first + $second;
echo ", $next";
$first = $second;
$second = $next;
}
?>

8. Develop a program to find sum of digit of any given number.

Ans:

<html>
<body>
<form action="sum_digits.php" method="post">
Enter the number : <input type="number" name="t1"><br>
<input type="submit" name="btn" value="Submit">
</form>
<?php
if(isset($_POST["btn"]))
{
$no=$_POST["t1"];
$sum=0;
$rem=0;
while($no>0)
{
$rem=$no%10;
$sum=$sum+$rem;
$no=$no/10;
}
echo "Sum of digits : ".$sum;
}
?>
</html>
</body>

9. Develop a program to reverse the digit of any given number.

Ans:

<html>
<body>
<form action="reverse.php" method="post">
Enter the number : <input type="number" name="t1"><br>
<input type="submit" name="btn" value="Submit">
</form>
<?php
if(isset($_POST["btn"]))
{
$no=$_POST["t1"];
$rev=0;
$rem=0;
while($no>0)
{
$rem=$no%10;
$rev=$rev*10+$rem;
$no=(int)($no/10);
}
echo "Reverse of number : ".$rev;
}
?>
</html>
</body>

10. Write a program to find given string is palindrome or not.

Ans:

<html>
<body>
<form action="str_palin.php" method="post">
Enter a string: <input type="text" name="t1"><br>
<input type="submit" name="btn" value="Check Palindrome">
</form>

<?php
if(isset($_POST["btn"]))
{
$str = $_POST["t1"];
$original=$str;
$reverse = strrev($original);
if($original == $reverse)
{
echo "<p><strong>$str</strong> is a palindrome.</p>";
}
else
{
echo "<p><strong>$str</strong> is not a palindrome.</p>";
}
}
?>
</body>
</html>

OR

<html>
<body>
<form action="str_palin.php" method="post">
Enter a string: <input type="text" name="t1"><br>
<input type="submit" name="btn" value="Check Palindrome">
</form>

<?php
if(isset($_POST["btn"]))
{
$str = $_POST["t1"];
$len = strlen($str);
$rev = "";

// Reversing manually
for($i = $len - 1; $i >= 0; $i--)
{
$rev .= $str[$i]; //$rev = $rev . $str[$i];
}

if($str == $rev)
{
echo "<p><strong>$str</strong> is a palindrome.</p>";
}
else
{
echo "<p><strong>$str</strong> is not a palindrome.</p>";
}
}
?>
</body>
</html>

11. Write a program to find given number is palindrome or not.

Ans:

<html>
<body>
<form action="palin.php" method="post">
Enter the number : <input type="number" name="t1"><br>
<input type="submit" name="btn" value="Submit">
</form>
<?php
if(isset($_POST["btn"]))
{
$no=$_POST["t1"];
$num=$no;
$rev=0;
$rem=0;
while($no>0)
{
$rem=$no%10;
$rev=$rev*10+$rem;
$no=(int)($no/10);
}
if($num==$rev)
{
echo "Number is palindrome";
}
else
{
echo "Number is not palindrome";
}
}
?>
</html>
</body>

12. Write a program to display following output


1
12
123

Ans:

<html>
<body>
<h3>Number Pattern</h3>

<?php
for($i = 1; $i <= 3; $i++) // Outer loop for rows
{
for($j = 1; $j <= $i; $j++) // Inner loop for numbers in each row
{
echo $j . " ";
}
echo "<br>"; // Move to next line after each row
}
?>

</body>
</html>
13. Write a program to display following output
1
23
456

Ans:

<html>
<body>
<h3>Number Pattern</h3>

<?php
$count = 1;

for($i = 1; $i <= 3; $i++) // Outer loop for 3 rows


{
for($j = 1; $j <= $i; $j++) // Inner loop prints numbers in each row
{
echo $count . " ";
$count++; // Increment number after each print
}
echo "<br>";
}
?>

</body>
</html>

14. Write a program to display following output


a
ab
abc

Ans:

<html>
<body>
<h3>Alphabet Pattern</h3>

<?php
for($i = 1; $i <= 3; $i++) // Rows
{
$ch = 'a';

for($j = 1; $j <= $i; $j++) // Columns


{
echo $ch . " ";
$ch++; // Move to next alphabet
}

echo "<br>"; // New line after each row


}
?>

</body>
</html>

15. Write a program to display following output


a
bc
def

Ans:

<html>
<body>
<h3>Alphabet Pattern</h3>

<?php
$ch = 'a';

for($i = 1; $i <= 3; $i++) // Outer loop for 3 rows


{
for($j = 1; $j <= $i; $j++) // Inner loop for columns
{
echo $ch . " ";
$ch++; // Go to next character
}
echo "<br>"; // Move to next line
}
?>

</body>
</html>

16. Write a program to create associative array and print this array.

Ans:

<html>
<body>
<h3>Associative Array Example</h3>

<?php
// Creating an associative array
$student = array(
"name" => "John",
"age" => 20,
"course" => "Computer Science"
);

// Printing the associative array using foreach


echo "<b>Student Details:</b><br>";
foreach($student as $key => $value)
{
echo $key . " : " . $value . "<br>";
}
?>

</body>
</html>

17. Write a program to create multidimensional array and display array elements.

Ans:

<html>
<body>
<h3>Multidimensional Array Example</h3>

<?php
// Creating a 2D associative array
$students = array(
array("name" => "John", "age" => 20, "course" => "Computer Science"),
array("name" => "Alice", "age" => 22, "course" => "Mathematics"),
array("name" => "Bob", "age" => 21, "course" => "Physics")
);

// Displaying array elements


echo "<b>Student Details:</b><br>";

foreach ($students as $student)


{
echo "Name: " . $student["name"] . "<br>";
echo "Age: " . $student["age"] . "<br>";
echo "Course: " . $student["course"] . "<br><br>";
}
?>

</body>
</html>

18. Write a program to calculate sum of array elements


Ans:

<html>
<body>
<h3>Sum of Array Elements</h3>

<?php

$numbers = array(10, 20, 30, 40, 50);

$sum = array_sum($numbers);

echo "The sum of the array elements is: " . $sum;


?>

</body>
</html>

OR

<html>
<body>
<h3>Sum of Array Elements (Using Loop)</h3>

<?php

$numbers = array(10, 20, 30, 40, 50);

$sum = 0;

foreach ($numbers as $num) {


$sum += $num;
}

echo "The sum of the array elements is: " . $sum;


?>

</body>
</html>

19. Write a program to calculate length of string without using string function.

Ans:

<html>
<body>
<?php

$string = "Hello World";


$length = 0;

while (isset($string[$length]))
{
$length++;
}

echo "The length of the string is: " . $length;

?>

</body>
</html>

20. Write a program to calculate number of words without using string function.

Ans:

<html>
<body>
<?php
$string = "Hello, how are you doing today?";

$words = explode(' ', $string);

$wordCount = count($words);

echo "Number of words: " . $wordCount;


?>

</body>
</html>

21. Develop a program to find sum of digit of any given number using function.

Ans:

<html>
<body>
<form action="sum_function.php" method="post">
Enter the number : <input type="number" name="t1"><br>
<input type="submit" name="btn" value="Submit">
</form>

<?php

function sum_digits($no) {
$sum = 0;
while ($no > 0) {
$rem = $no % 10;
$sum += $rem;
$no = (int)($no / 10);
}
return $sum;
}

if (isset($_POST["btn"])) {
$no = $_POST["t1"];
$sum = sum_digits($no);
echo "Sum of digits: " . $sum;
}

?>

</body>
</html>

22. Develop a program to reverse the digit of any given number using function.

Ans:

<html>
<body>
<form action="reverse_function.php" method="post">
Enter the number : <input type="number" name="t1"><br>
<input type="submit" name="btn" value="Submit">
</form>

<?php

function reverse_digits($no) {
$rev = 0;
while ($no > 0) {
$rem = $no % 10;
$rev = $rev * 10 + $rem;
$no = (int)($no / 10);
}
return $rev;
}

if (isset($_POST["btn"])) {
$no = $_POST["t1"];
$reversed_number = reverse_digits($no);
echo "Reversed number: " . $reversed_number;
}
?>

</body>
</html>

23. Write a program to find given string is palindrome or not using function.

Ans:

<html>
<body>
<form action="str_palin_fun.php" method="post">
Enter the string: <input type="text" name="t1"><br>
<input type="submit" name="btn" value="Submit">
</form>

<?php
function is_palindrome($str) {
$reversed = strrev($str);
if ($str === $reversed) {
return true;
} else {
return false;
}
}

if (isset($_POST["btn"])) {
$str = $_POST["t1"];
if (is_palindrome($str)) {
echo "The string is a palindrome.";
} else {
echo "The string is not a palindrome.";
}
}
?>

</body>
</html>

24. Write a program to find given number is palindrome or not using function.

Ans:

<html>
<body>
<form action="palindrom.php" method="post">
Enter the number: <input type="number" name="t1"><br>
<input type="submit" name="btn" value="Submit">
</form>

<?php
function is_palindrome($n) {
$orig = $n;
$rev = 0;
while ($n > 0) {
$rem = $n % 10;
$rev = $rev * 10 + $rem;
$n = (int)($n / 10);
}
return $orig ==$rev;
}

if (isset($_POST["btn"])) {
$n = $_POST["t1"];
if (is_palindrome($n)) {
echo "The number is a palindrome.";
} else {
echo "The number is not a palindrome.";
}
}
?>

</body>
</html>

25. Write a program to implement parameterized constructor.

Ans:

<?php
class Student {
public $name;
public $age;

function __construct($n, $a) {


$this->name = $n;
$this->age = $a;
}

function display() {
echo "Name: " . $this->name . "<br>";
echo "Age: " . $this->age;
}
}

$obj = new Student("Amit", 20);


$obj->display();
?>

26. Write a program to implement inheritance and constructor overloading.

Ans:

<?php
class student
{
function __construct($r)
{
$this->roll=$r;

}
function display()
{
print("Roll Number : $this->roll<br>");

}
}

class name extends student


{
function __construct($r,$n)
{
parent::__construct($r);
$this->name=$n;
}
function display()
{
parent::display();
print("Name : $this->name");
}
}

$n=new name(2,"ABC");
$n->display();
?>

27. Write a program to implement interface.

Ans:

<?php
class Person {
public $name, $roll;

function setData($n, $r) {


$this->name = $n;
$this->roll = $r;
}
}

interface Result {
public function setPercentage($p);
}

class Student extends Person implements Result {


public $per;

public function setPercentage($p) {


$this->per = $p;
}

public function show() {


echo "Name: $this->name<br>";
echo "Roll No: $this->roll<br>";
echo "Percentage: $this->per%";
}
}

$obj = new Student();


$obj->setData("Amit", 101);
$obj->setPercentage(92.5);
$obj->show();
?>

28. Write a program to create image with text.

Ans:

<?php
Header('Content-Type:image/png');
$img=ImageCreate(500,500);
$bg_color=ImageColorAllocate($img,255,255,0);
$txt_color=ImageColorAllocate($img,0,0,0);
imagefill($img,0,0,$bg_color);
imageString($img,10,180,200,"Welcome to PHP",$txt_color);
imageString($img,10,90,220,"It is server side scripting language",$txt_color);
imagepng($img);
?>

29. Write a program to create pdf file in PHP.

Ans:

<?php
require('fpdf186/fpdf.php'); //directly use only fpdf.php
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(60,10,'Hello PHP World!',1,1,'C');
$pdf->output();
?>

30. Write a simple PHP program to demonstrate the use of built in string function.

Ans:

<?php
$text = " Hello, welcome to the PHP world! ";

$wordCount = str_word_count($text);
echo "Number of words: " . $wordCount . "<br>";

$stringLength = strlen($text);
echo "Length of the string: " . $stringLength . "<br>";

$reversedText = strrev($text);
echo "Reversed string: " . $reversedText . "<br>";

$position = strpos($text, "PHP");


echo "Position of 'PHP' in the string: " . $position . "<br>";

$replacedText = str_replace("PHP", "Java", $text);


echo "Replaced string (PHP -> Java): " . $replacedText . "<br>";

$capitalizedText = ucwords($text);
echo "String with capitalized words: " . $capitalizedText . "<br>";

$upperText = strtoupper($text);
echo "Uppercase string: " . $upperText . "<br>";

$lowerText = strtolower($text);
echo "Lowercase string: " . $lowerText . "<br>";
?>

31. Write a simple program on Introspection and Serialization.

Ans:

<?php

class Demo {
public function getData($name) {
return "Hello, " . $name . "!";
}

public function anotherMethod() {


return "This is another method!";
}
}

if (class_exists('Demo')) {
echo "Class 'Demo' exists!<br>";

$demo = new Demo();


echo "Class name of the object: " . get_class($demo) . "<br>";

$methods = get_class_methods('Demo');
echo "Methods in the 'Demo' class: <br>";
foreach ($methods as $method) {
echo $method . "<br>";
}

echo $demo->getData("John") . "<br>";


} else {
echo "Class 'Demo' does not exist.<br>";
}

$s_data=serialize(array('welcome','to','php'));
print_r($s_data."<br>");
$us_data=unserialize($s_data);
print_r($us_data);

?>

32. Write a PHP program to create registration form having Name, Address, Mobile No, Gender
(Radio Button) and class and validate a registration form.

Ans:

<html>
<body>

<h2>Registration Form</h2>
<form method="post" action="form.php">
Name: <input type="text" name="name" value=""><br><br>

Address: <textarea name="address"></textarea><br><br>

Mobile: <input type="text" name="mobile" value=""><br><br>


Gender:
<input type="radio" name="gender" value="Male"> Male
<input type="radio" name="gender" value="Female"> Female<br><br>

Class:
<select name="class">
<option value="">Select Class</option>
<option value="Class 1">FY</option>
<option value="Class 2">SY</option>
<option value="Class 3">TY</option>
</select><br><br>

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


</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {

if (empty($_POST["name"])) {
echo "Name can't be blank<br>";
}

if (empty($_POST["address"])) {
echo "Address can't be blank<br>";
}

if (empty($_POST["mobile"])) {
echo "Mobile number can't be blank<br>";
} elseif (!preg_match("/^[0-9]{10}$/", $_POST["mobile"])) {
echo "Invalid mobile number (must be 10 digits)<br>";
}

if (empty($_POST["gender"])) {
echo "Gender can't be blank<br>";
}

if (empty($_POST["class"])) {
echo "Class can't be blank<br>";
}
}
?>

</body>
</html>

33. Write a PHP program to create exam form having Enrollment No, Name, Subject (check box),
Fee, class and validate a registration form.

Ans:
<html>
<body>

<h2>Exam Registration Form</h2>


<form method="post" action="exam_form.php">
Enrollment No: <input type="text" name="enrollment_no"><br><br>

Name: <input type="text" name="name"><br><br>

Subjects:
<input type="checkbox" name="subjects[]" value="Math"> Math
<input type="checkbox" name="subjects[]" value="Science"> Science
<input type="checkbox" name="subjects[]" value="English"> English<br><br>

Fee: <input type="text" name="fee"><br><br>

Class:
<select name="class">
<option value="">Select Class</option>
<option value="Class 1">FY</option>
<option value="Class 2">SY</option>
<option value="Class 3">TY</option>
</select><br><br>

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


</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$subjects = [];

if (empty($_POST["enrollment_no"])) {
echo "Enrollment No is required<br>";
}

if (empty($_POST["name"])) {
echo "Name is required<br>";
}

if (empty($_POST["subjects"])) {
echo "At least one subject must be selected<br>";
}

if (empty($_POST["fee"])) {
echo "Fee is required<br>";
}

if (empty($_POST["class"])) {
echo "Class is required<br>";
}

}
?>

</body>
</html>

34. Write a PHP program to create Employee Registration form having Emp Code, Name,
Department (select), Payment, class and validate a registration form.

Ans:

<html>
<body>

<h2>Employee Registration Form</h2>


<form method="post" action="">
Emp Code: <input type="text" name="emp_code"><br><br>

Name: <input type="text" name="name"><br><br>

Department:
<select name="department">
<option value="">Select Department</option>
<option value="HR">HR</option>
<option value="IT">IT</option>
<option value="Finance">Finance</option>
<option value="Sales">Sales</option>
</select><br><br>

Payment: <input type="text" name="payment"><br><br>

Class:
<select name="class">
<option value="">Select Class</option>
<option value="Class 1">Junior</option>
<option value="Class 2">Senior</option>
</select><br><br>

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


</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["emp_code"])) {
echo "Emp Code is required<br>";
}
if (empty($_POST["name"])) {
echo "Name is required<br>";
}

if (empty($_POST["department"])) {
echo "Department is required<br>";
}

if (empty($_POST["payment"])) {
echo "Payment is required<br>";
}

if (empty($_POST["class"])) {
echo "Class is required<br>";
}

// If all fields are filled correctly, display the registration details(Optional)


if (!empty($_POST["emp_code"]) && !empty($_POST["name"]) && !
empty($_POST["department"]) && !empty($_POST["payment"]) && !
empty($_POST["class"])) {
echo "<h3>Registration Successful!</h3>";
echo "Emp Code: " . $_POST["emp_code"] . "<br>";
echo "Name: " . $_POST["name"] . "<br>";
echo "Department: " . $_POST["department"] . "<br>";
echo "Payment: " . $_POST["payment"] . "<br>";
echo "Class: " . $_POST["class"] . "<br>";
}
}
?>

</body>
</html>

35. Write a PHP program to create registration form having Name, Address, Mobile No, Gender
(Radio Button) and class and display data.

Ans:

36. Write a PHP program to create exam form having Enrollment No, Name, Subject (check box),
Fee, class and display data.
37. Write a PHP program to create Employee Registration form having Emp Code, Name,
Department (select), Payment, class and display data.
38. Write a PHP program to create Cookies, set value and read cookies data.

Ans:

<?php
$cookie_name = "user_name";
$cookie_value = "JohnDoe";
$cookie_time = 3600; // Cookie will expire in 1 hour

// Create a cookie
if (isset($_POST['create_cookie'])) {
echo "Cookie values are set. You can now set the cookie.<br><br>";
}

// Set a cookie
if (isset($_POST['set_cookie'])) {
// Set the cookie with the values
setcookie($cookie_name, $cookie_value, time() + $cookie_time, '/');
echo "Cookie '$cookie_name' has been set with value '$cookie_value' and will expire in
$cookie_time seconds.<br><br>";
}

// Read the cookie


if (isset($_POST['read_cookie'])) {
if (isset($_COOKIE[$cookie_name])) {
echo "Cookie '$cookie_name' is: " . $_COOKIE[$cookie_name] . "<br><br>";
} else {
echo "Cookie '$cookie_name' is not set.<br><br>";
}
}
?>

<html>
<body>

<h2>Cookie Operations</h2>

<form method="post" action="">


<input type="submit" name="create_cookie" value="Create Cookie"><br><br>
</form>

<form method="post" action="">


<input type="submit" name="set_cookie" value="Set Cookie"><br><br>
</form>

<form method="post" action="">


<input type="submit" name="read_cookie" value="Read Cookie"><br><br>
</form>

</body>
</html>
39. Write a PHP program to create Cookies, set value and expire cookies.

Ans:

<?php
$cookie_name = "user_name";
$cookie_value = "JohnDoe";
$cookie_time = 3600; // 1 hour

// Create cookie message


if (isset($_POST['create_cookie'])) {
echo "Cookie variables are ready to be set.<br><br>";
}

// Set cookie
if (isset($_POST['set_cookie'])) {
setcookie($cookie_name, $cookie_value, time() + $cookie_time, "/");
echo "Cookie '$cookie_name' set with value '$cookie_value'.<br><br>";
}

// Expire cookie
if (isset($_POST['expire_cookie'])) {
setcookie($cookie_name, "", time() - 3600, "/"); // set time in past to delete
echo "Cookie '$cookie_name' has been expired.<br><br>";
}
?>

<html>
<body>
<h2>Cookie Operations</h2>

<form method="post">
<input type="submit" name="create_cookie" value="Create Cookie"><br><br>
</form>

<form method="post">
<input type="submit" name="set_cookie" value="Set Cookie"><br><br>
</form>

<form method="post">
<input type="submit" name="expire_cookie" value="Expire Cookie"><br><br>
</form>

</body>
</html>

40. Create customer form for customer name, address, mobile no, item_purchase, amount using
different form input element & display user inserted values in new PHP form.
Ans:

<html>
<body>

<h2>Customer Purchase Form</h2>


<form action="customer.php" method="post">
Customer Name: <input type="text" name="name"><br><br>

Address: <textarea name="address"></textarea><br><br>

Mobile No: <input type="number" name="mobile"><br><br>

Item Purchased:
<select name="item">
<option value="Laptop">Laptop</option>
<option value="Mobile">Mobile</option>
<option value="Headphones">Headphones</option>
<option value="Smart Watch">Smart Watch</option>
</select><br><br>

Amount: <input type="number" name="amount"><br><br>

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


</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
echo "<h2>Customer Details:</h2>";
echo "Name: " . $_POST['name'] . "<br><br>";
echo "Address: " . $_POST['address'] . "<br><br>";
echo "Mobile No: " . $_POST['mobile'] . "<br><br>";
echo "Item Purchased: " . $_POST['item'] . "<br><br>";
echo "Amount: Rs. " . $_POST['amount'] . "<br><br>";
} else {
echo "Form not submitted.";
}
?>

</body>
</html>

41. Create Employee form for Employee name, Address, Mobile no, Date of Birth, Post & salary
using different form input element & display user inserted values in same PHP form.

Ans:

<html>
<body>
<h2>Employee Registration Form</h2>
<form method="post" action="">
Employee Name: <input type="text" name="name"><br><br>

Address: <textarea name="address"></textarea><br><br>

Mobile No: <input type="number" name="mobile"><br><br>

Date of Birth: <input type="date" name="dob"><br><br>

Post:
<select name="post">
<option value="Manager">Manager</option>
<option value="Developer">Developer</option>
<option value="Tester">Tester</option>
<option value="HR">HR</option>
</select><br><br>

Salary: <input type="number" name="salary"><br><br>

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


</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
echo "<h3>Employee Details:</h3>";
echo "Name: " . $_POST['name'] . "<br><br>";
echo "Address: " . $_POST['address'] . "<br><br>";
echo "Mobile No: " . $_POST['mobile'] . "<br><br>";
echo "Date of Birth: " . $_POST['dob'] . "<br><br>";
echo "Post: " . $_POST['post'] . "<br><br>";
echo "Salary: Rs. " . $_POST['salary'] . "<br><br>";
}
?>

</body>
</html>

42. Write a PHP script to read Account information for customer name, account_no,
account_type, branch_name, city, amount from account table and display this information.

Ans:

<?php
$host = "localhost";
$user = "root";
$password = "";
$db = "bank";
$conn = new mysqli($host, $user, $password, $db);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT customer_name, account_no, account_type, branch_name, city, amount


FROM account";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
echo "<h2>Account Information</h2>";

while($row = $result->fetch_assoc()) {
echo "Customer Name: " . $row["customer_name"] . "<br>";
echo "Account No: " . $row["account_no"] . "<br>";
echo "Account Type: " . $row["account_type"] . "<br>";
echo "Branch Name: " . $row["branch_name"] . "<br>";
echo "City: " . $row["city"] . "<br>";
echo "Amount: Rs. " . $row["amount"] . "<br><br>";
}
} else {
echo "No account records found.";
}

$conn->close();
?>

43. Write a PHP script to insert one record in student registration table (roll_no,
name,city,mobile_no) in mysql database.

Ans:

<?php
$host = "localhost";
$user = "root";
$password = "";
$db = "bank"; // Replace with your DB name

$conn = new mysqli($host, $user, $password, $db);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$roll_no = 1;
$name = "Rahul Sharma";
$city = "Mumbai";
$mobile_no = "9876543210";

$sql = "INSERT INTO student_registration (roll_no, name, city, mobile_no)


VALUES ($roll_no, '$name', '$city', '$mobile_no')";

if ($conn->query($sql) === TRUE) {


echo "Record inserted successfully!";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

You might also like