1 2
1 2
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>
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>
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>
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>
Ans:
<?php
$sum=0;
for($i=1;$i<=12;$i++)
{
$sum=$sum+$i;
}
echo "Sum of first 12 numbers : ".$sum;
?>
Ans:
<?php
$first = 0;
$second = 1;
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>
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>
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>
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>
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;
</body>
</html>
Ans:
<html>
<body>
<h3>Alphabet Pattern</h3>
<?php
for($i = 1; $i <= 3; $i++) // Rows
{
$ch = 'a';
</body>
</html>
Ans:
<html>
<body>
<h3>Alphabet Pattern</h3>
<?php
$ch = 'a';
</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"
);
</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")
);
</body>
</html>
<html>
<body>
<h3>Sum of Array Elements</h3>
<?php
$sum = array_sum($numbers);
</body>
</html>
OR
<html>
<body>
<h3>Sum of Array Elements (Using Loop)</h3>
<?php
$sum = 0;
</body>
</html>
19. Write a program to calculate length of string without using string function.
Ans:
<html>
<body>
<?php
while (isset($string[$length]))
{
$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?";
$wordCount = count($words);
</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>
Ans:
<?php
class Student {
public $name;
public $age;
function display() {
echo "Name: " . $this->name . "<br>";
echo "Age: " . $this->age;
}
}
Ans:
<?php
class student
{
function __construct($r)
{
$this->roll=$r;
}
function display()
{
print("Roll Number : $this->roll<br>");
}
}
$n=new name(2,"ABC");
$n->display();
?>
Ans:
<?php
class Person {
public $name, $roll;
interface Result {
public function setPercentage($p);
}
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);
?>
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>";
$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>";
?>
Ans:
<?php
class Demo {
public function getData($name) {
return "Hello, " . $name . "!";
}
if (class_exists('Demo')) {
echo "Class 'Demo' exists!<br>";
$methods = get_class_methods('Demo');
echo "Methods in the 'Demo' class: <br>";
foreach ($methods as $method) {
echo $method . "<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>
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>
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>
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>
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>
<?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>
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>
Class:
<select name="class">
<option value="">Select Class</option>
<option value="Class 1">Junior</option>
<option value="Class 2">Senior</option>
</select><br><br>
<?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>";
}
</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>";
}
<html>
<body>
<h2>Cookie Operations</h2>
</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
// 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>
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>
<?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>
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>
<?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);
}
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
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$roll_no = 1;
$name = "Rahul Sharma";
$city = "Mumbai";
$mobile_no = "9876543210";
$conn->close();
?>