PHP PROGRAMS
1. Continue & Break
Continue
<?php
for($i=1; $i<=5; $i++)
{
if($i==3)
{
continue;
}
echo "$i<br>";
}
?>
Break
<?php
for($i=1; $i<=5; $i++)
{
echo "$i<br>";
if($i==3)
{
break;
}}
?>
2. For Loop & Foreach loop
For Loop
<?php
$sum=0;
for($i=0; $i<=10; $i+=2)
{
echo "$i<br>";
$sum +=$i;
}
echo "Sum: $sum";
?>
Foreach Loop
<?php
$arr = array (10,20,30,40);
foreach ($arr as $i)
{
echo "$i<br>";
}
?>
3. WAP to Implement Array
Indexed Array Program :-
<?php
$course[0]="Computer Engg";
$course[1]="Information Technology";
$course[2]="Electronics and Telecomm";
echo $course[2].="<BR>";
echo $course[0].="<BR>";
echo $course[1].="<BR>";
?>
Associative Array Program :-
<?php
$capital=array("Mumbai" => "Maharashtra","Goa" => "Panaji","Bihar"=>
"Patna");
print_r($capital);
echo "<br>";
echo "Mumbai is a Capital of " .$capital["Mumbai"];
?>
Multidimensional Array Program:-
<?php
$person=array(
array(
"name" => "Yogita K",
"mob" => "5689741523",
"email" => "
[email protected]",
),
array(
"name" => "Manisha P.",
"mob" => "2584369721",
"email" => "
[email protected]",
),
array(
"name" => "Vijay Patil",
"mob" => "9875147536",
"email" => "
[email protected]",
)
);
echo "manisha P's email-id is: " .$person[1]["email"],
"<br>";
echo "Vijay Patil's mobile no: " .$person[2]["mob"];
?>
4. WAP to Implement Implode() & Explode() Function
Implode() Function
<?php
$course[0]="Computer Engg.";
$course[1]="Information Tech.";
$course[2]="Electronics and Telecomm.";
$text=implode(",",$course);
echo $text;
?>
Explode() Function
<?php
$str="PHP: Welcome to the world of PHP";
print_r(explode(" ",$str));
?>
5. WAP to Implement Array Function
Array_flip()
<?php
$a1=array("CO"=>"Computer Engg","IF"=>"Information
Tech","EJ"=>"Electronics and Telecomm");
$result=array_flip($a1);
print_r($result);?>
6. WAP to Implement String functions
Strlen()
<?php
echo strlen("welcome to PHP world");
?>
Strrev()
<?php
echo strrev("Information Technology");
?>
Strtoupper()
<?php
echo strtoupper ("welcome to PHP world");
?>
Strtolower()
<?php
echo strtolower ("welcome to PHP world");
?>
7. Variable function & Anonymous function
Variable Func
<?php
function simple()
{
echo "In Simple()<br>\n";
}
function data($arg="")
{
echo "In data(); argument is $arg<br>\n";
}
$func = 'simple';
$func();
$func = 'data';
$func('test');
?>
Anonymous Func
<?php
$str = "Hello World!";
$func = function($match)
{
return "PHP!";
};
$str = preg_replace_callback('/World/', $func, $str);
echo $str;
?>
8. PDF in PHP
<?php
require('fpdf.php');
$pdf = new FPDF();
$pdf -> AddPage();
$pdf -> SetFont('Arial','B','16');
$pdf -> Cell(60,10,'Hello PHP World!',1,1,'C');
$pdf -> Output();
?>
9. WAP to Implement Constructor
<?php
class MyClass
{
function construct()
{
echo "Welcome to PHP constructor.<br/>";
}
}
$obj = new MyClass;
$obj->construct();
?>
10.WAP to Implement Inheritance (all types)
i. Single Inheritance
<?php
class a
{
function dis1()
{
echo "Hello PHP<br>";
}
}
class b extends a
{
function dis2()
{
echo "PHP Programming<br>";
}
}
$obj = new b();
$obj -> dis1();
$obj -> dis2();
?>
ii. Multilevel Inheritance
<?php
class grandparent
{
function dis1()
{
echo"Grand-Parents <br>";
}
}
class parents extends grandparent
{
function dis2()
{
echo"Parents <br>";
}
}
class child extends parents
{
function dis3()
{
echo "Child <br>";
}
}
$obj=new child();
$obj->dis1();
$obj->dis2();
$obj->dis3();
?>
iii. Hierarchical Inheritance
<?php
class a
{
public function function_a()
{
echo "class A<br>";
}
}
class b extends a
{
public function function_b()
{
echo "class B<br>";
}
}
class c extends a
{
public function function_c()
{
echo "class C<br>";
}
}
$obj=new c();
$obj->function_c();
$obj->function_a();
?>
iv. Multiple Inheritance
<?php
//Class A i.e Parent A
class A{
public function dis1()
{
echo "Parent-A <br>";
}
}
//Trait B i.e Parent B
trait B
{
public function dis2()
{
echo "Parent-B <br>";
}
}
class C extends A
{
use B;
public function dis3()
{
echo"\nChild-C";
}
}
$obj=new C();
$obj->dis1();
$obj->dis2();
$obj->dis3();
?>
11.WAP to Implement Interface
<?php
class A
{
public function dis1()
{
echo "Parent-A<br>";
}}
interface B
{
public function dis2();
}
class C extends A implements B
{
function dis2()
{
echo "Parent-B<br>";
}
public function dis3()
{
echo "\nParent-C";
}}
$obj = new C();
$obj -> dis1();
$obj -> dis2();
$obj -> dis3();
?>
12.Abstract Method & Classes
<?php
abstract class base
{
abstract function printdata();
}
class Derived extends base
{
function printdata()
{
echo "Derived Class";
}}
$obj = new Derived;
$obj -> printdata();
?>
13. Method Overriding
<?php
class Shape
{
public $length;
public $width;
public function_construct ($length,$width)
{
$this->length=$length;
$this->width=$width;
}
public function intro()
{
echo "The length is" .$this->length. "and the width is" .$this->width;
}
}
class Square extends Shape
{
public $height;
public function_construct ($length,$width,$height)
{
$this->length=$length;
$this->width=$width;
$this->width=$height;
}
public function intro()
{
echo "The length is".$this->length. ", the width is" .$this->width. "and the
height is" .$this->height;
}
}
$s = new Square(20,40,60);
$s->intro();
?>
14. Introspection & Serialization
Introspection
<?php
if(class_exists('Demo'))
{
$demo = new Demo();
echo "This is demo class";
}
else
{
echo "Class does not exists";
}
?>
Serialization
<?php
$s_data = serialize(array('Welcome','to','PHP'));
print_r($s_data."<br>");
$us_data = unserialize($s_data);
print_r($us_data);
?>
15.Get & Post method
Get Method
GetMethod.html
<!DOCTYPE html>
<html>
<head>
<title>Document</title>
</head>
<body>
<form action="GetMethod.php" method="get">
Name: <input type="text" name="name">
Address: <input type="text" name="address">
<input type="submit" name="Submit">
</form>
</body>
</html>
GetMethod.php
<html>
<body>
Welcome: <?php echo $_GET["name"]; ?>!
<br>
Your address is: <?php echo $_GET["address"]; ?>
</body>
</html>
Post Method
PostMethod.html
<!DOCTYPE html>
<html>
<head>
<title>Document</title>
</head>
<body>
<form action="PostMethod.php" method="post">
Name: <input type="text" name="name" id="name">
<br>
Address: <input type="text" name="address" id="address">
<br>
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
PostMethod.php
<html>
<body>
Welcome: <?php echo $_POST["name"]; ?>!
<br>
Your address is: <?php echo $_POST["address"]; ?>
</body>
</html>
16.WAP to Implement student admission form using controls of <form> in
php
<html>
<head>
<title>Student Admission Form</title>
</head>
<body>
<h1>Student Admission Form</h1>
<form action="process_admission.php" method="post">
<label for="name">Name:</label>
<input type="text" name="name" required><br>
<label for="email">Email:</label>
<input type="email" name="email" required><br>
<label for="phone">Phone:</label>
<input type="tel" name="phone" required><br>
<label for="dob">Date of Birth:</label>
<input type="date" name="dob" required><br>
<label for="address">Address:</label>
<textarea name="address" rows="5" cols="30"
required></textarea><br>
<label for="course">Course:</label>
<select name="course" required>
<option value="">-- Select a course --</option>
<option value="B.Tech">B.Tech</option>
<option value="B.Sc">B.Sc</option>
<option value="B.Com">B.Com</option>
<option value="BBA">BBA</option>
</select><br>
<label for="gender">Gender:</label>
<input type="radio" name="gender" value="male" required> Male
<input type="radio" name="gender" value="female" required>
Female
<input type="radio" name="gender" value="other" required>
Other<br>
<label for="terms">Agree to Terms:</label>
<input type="checkbox" name="terms" value="agree"
required><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
process_admission.php
<html>
<head>
<title>Processing Admission Form</title>
</head>
<body>
<?php
// Get form data
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$dob = $_POST['dob'];
$address = $_POST['address'];
$course = $_POST['course'];
$gender = $_POST['gender'];
// Validate form data
if (empty($name) || empty($email) || empty($phone) || empty($dob) ||
empty($address) || empty($course) || empty($gender)) {
echo "<p>Missing required form data. Please go back and try
again.</p>";
} else {
// Process form data
echo "<h1>Thank you for submitting the student admission form!
</h1>";
echo "<p>Name: " . $name . "</p>";
echo "<p>Email: " . $email . "</p>";
echo "<p>Phone: " . $phone . "</p>";
echo "<p>Date of Birth: " . $dob . "</p>";
echo "<p>Address: " . $address . "</p>";
echo "<p>Course: " . $course . "</p>";
echo "<p>Gender: " . $gender . "</p>";
}
?>
</body></html>
17.WAP for web page validation
Program 1:- valid1.html
<html>
<head>
<title>Validating Form Data</title>
</head>
<body>
<form method="post" action="valid2.php">
Name:<input type="text" name="name" id="name"/><br/>
Mobile Number:<input type="text" name="mobileno" id="mobileno"/><br/>
Email ID:<input type="text" name="email" id="email"/><br/>
<input type="submit" name="submit_btn" value="Submit"/>
</form>
</body>
</html>
Program 2:- valid2.php
<?php
if($_SERVER['REQUEST_METHOD']==='POST')
{
if(empty($_POST['name']))
{
echo "Name can't be blank<br/>";
}
if(!is_numeric($_POST['mobileno']))
{
echo "Enter valid Mobile Number<br/>";
}
$pattern='^b[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}\b/';
if(!preg_match($pattern,$_POST['email']))
{
echo "Enter valid email id.<br>";
}
}
?>
18.WAP to create cookie
<html>
<body>
<?php
$cookie_name= "username";
$cookie_value="abc";
setcookie($cookie_name,$cookie_value,time()+(86400 * 30),"/");
if(!isset($_COOKIE[$cookie_name]))
{
echo "Cookie name". $cookie_name . "is not set!";
}
else
{
echo "cookie" .$cookie_name . "is set! <br>";
echo "value is: " .$_COOKIE[$cookie_name];
}
?>
</body>
</html>
19.DATABASE
<?php
$hn = "localhost";
$db = "Student";
$un = "root";
$pw = "";
$conn = new mysqli($hn,$un,$pw,$db);
if($conn->connect_error)die($conn->connect_error);
$query = "INSERT INTO Data(Name,R_no,Age,Addr)Values(‘UBAID
AJMERI’,20801,18,’MUMBAI’)";
$result = $conn->query($query);
if(!$result)
{
die("Database access failed".$conn->error);
}
else
{
echo"Success!";
}
20.WAP to create Session
Set Session Variable
<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Set session variables
$_SESSION["Name"] = "Vijay";
$_SESSION["Address"] = "Thane";
echo "Session variables are set.";
?>
</body>
</html>
Get Session Variable
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Echo session variables that were set on previous page
echo "User Name: ".$_SESSION["Name"]."<br>";
echo "User Address: ".$_SESSION["Address"]. ".";
?>
</body>
</html>