0% found this document useful (0 votes)
13 views25 pages

Untitled Document

The document provides various PHP programming examples, including comparison operators, logical operators, control structures like switch and loops, and array manipulations. It also covers object-oriented programming concepts such as constructors, inheritance, and demonstrates the use of built-in functions for strings and arrays. Additionally, it includes examples of form validation, email validation, and cookie management in PHP.

Uploaded by

vaijantachavle
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)
13 views25 pages

Untitled Document

The document provides various PHP programming examples, including comparison operators, logical operators, control structures like switch and loops, and array manipulations. It also covers object-oriented programming concepts such as constructors, inheritance, and demonstrates the use of built-in functions for strings and arrays. Additionally, it includes examples of form validation, email validation, and cookie management in PHP.

Uploaded by

vaijantachavle
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/ 25

⚫ Comparison operators in php }

with example else {


echo "invalid";
}
<?php ?>

Output:-
$a = 10;
$b = 20;

if ($a > $b) {


echo "$a is greater than $b\n";
}

else if ($a < $b) {


echo "$a is less than $b\n";
}

else if ($a >= $b) {


echo "$a is greater than or equal
to $b\n";
}

else if ($a <= $b) {


echo "$a is less than or equal to
$b\n";
}

else if ($a == $b) {


echo "$a is equal to $b\n";
}
else if ($a === $b) {
echo "$a is identical to $b\n";
}

else if ($a != $b) {


echo "$a is not equal to $b\n";
}
else if ($a !== $b) {
echo "$a is not identical to $b\n";
⚫ write various logical operators in
php with example
$month= 1;
<?php switch ($month)
{
$x = 10; case "1":
$y = 5; echo "January ";
$z=15; break;
case "2":
if ($x > $y && $x > $z) { echo "February ";
echo "$x greater "; break;
} case "3":
echo "March ";
if ($y > $x || $y > $z) { break;
echo "$y greater"; case "4":
} echo "April ";
break;
if (!($x < $y)) { case "5":
echo "x is not less than y "; echo "May ";
} break;
?> case "6":
echo "June ";
Output:- break;
case "7":
echo "July ";
break;
case "8":
echo "August ";
break;
case "9":
echo "September ";
break;
case "10":
echo "October ";
break;

⚫ write calendar program using case "11":


echo "November ";
switch statement break;
case "12":
echo "December ";
break;
<?php default:
echo "Invalid month echo $i;
number\n"; }
break; }
}
?>
?>
Output:-
Output:-

<?php

$i = 0;
while ($i <= 30)
{
if ($i % 2 == 0)
{
echo "$i<br>";
}
$i = $i + 1;
}

?>

Output:-

⚫ wap to print first 30 even


numbers using for ,while,do while

<?php

for($i=0;$i<=30;$i++)
{
if($i%2==0) <?php
{
$i=0;
do
{
echo "$i<br>";
$i=$i+2;
}
while($i<=30);

?>

⚫ develop program using


⚫ write a program to display
multidimensional array

pyramid of stars using increment <?php


decrement
$a=array("suraj"=>array("phy"=>20,
<?php "chem"=>30,"bio"=>50),"meeta"=>a
for($i=0;$i<=5;$i++) rray("phy"=>40,"chem"=>40,"bio"=>
{ 60),"megha"=>array("phy"=>30,"ch
em"=>40,"bio"=>50));
for($j=0;$j<=$i;$j++)
{
echo "*"; echo $a["suraj"]["phy"];
} echo "<br>";
echo "<br/>";
} echo $a["suraj"]["chem"];
?> echo "<br>";

echo $a["meeta"]["phy"];
Output:- echo "<br>";

echo $a["meeta"]["chem"];
echo "<br>";
echo $a["megha"]["phy"];
echo "<br>";

echo $a["megha"]["chem"];

?>

⚫ string function with example


<?php

⚫ develop a program using


$a="hello world";
echo str_word_count("Hello");
associative array echo "<br>";
echo strlen("Hello world!");
<?php echo "<br>";
echo strrev("Hello world!");
$a=array("seema"=>35,"veena"=>9 echo "<br>";
0,"vishakha"=>65); echo strpos($a,"hello");
print_r($a); echo "<br>";
echo str_replace("hello","bye",$a);
echo "<br>";
?> echo ucwords("hello world!");
echo "<br>";
Output:- echo strtoupper("Hello world!");
echo "<br>";
echo strtolower("Hello world!");
echo "<br>";
echo str_repeat("Hello world!",3);
echo "<br>";
echo strcmp("Hello world!","hello
world");
echo "<br>";
echo substr("Hello world!",1,4);
echo "<br>";
print_r (str_split($a));
echo "<br>";
echo str_shuffle($a);
echo "<br>";
echo chunk_split("hello
php",2,"....");

⚫ write a program to demonstrate


echo "<br>";
echo trim($a);
echo "<br>"; parameterized function
echo ltrim($a,"hello");
echo "<br>"; <?php
echo rtrim($a,"world");
echo "<br>"; function show($name,$rollno)
echo chop($a,"world"); {
echo "<br>"; return "name:$name,
rollno:$rollno";
}

?> echo show("janhavi",62);

Output: ?>

Output:-
⚫ demonstrate use of math
function

⚫ wap to demonstrate anonymous <?php


function

<?php echo abs(-7);


echo "<br>";
$c=function($a,$b)
{ echo ceil(3.3);
return $a+$b; echo "<br>";
};
echo $c(10,20); echo floor(-4.6);
echo "<br>";
?>
echo sqrt(16);
Output:- echo "<br>";

echo decbin(2);
echo "<br>";

echo dechex(10);
echo "<br>";

echo bindec(1000);
echo "<br>";

echo sin(3);
echo "<br>";

echo cos(3);
echo "<br>";

echo tan(10);
echo "<br>";

echo base_convert(1,2,10);
echo "<br>";

echo fmod(5,2);
echo "<br>";

$a=array(20,30,40,50,);
echo max(20,30,5,4,3);
echo "<br>"; echo $a[0] ,"<br>";
echo $a[1] ,"<br>";
echo min(1,3,4,5); echo $a[2] ,"<br>";
echo "<br>"; echo $a[3] ,"<br>";

echo pow(3,2); ?>


echo "<br>";

?> Output:-

Output:-

⚫check number is positive &


negative

<?php

⚫ develop a program using index $a=10;


array

if($a>0)
<?php {
echo "number is positive"; echo "<br>";
} print_r(array_intersect($a,$b));
if($a<0) echo "<br>";
{ print_r(array_reverse($a));
echo "number is negative"; echo "<br>";
} print_r(array_unique($b));
echo "<br>";
?> print_r(array_chunk($b,2));
echo "<br>";
print_r(array_count_values($b));
Output: echo "<br>";
print_r(array_pop($b));
echo "<br>";
print_r(array_sum($b));
echo "<br>";
print_r(array_push($b,30));
echo "<br>";
print_r(array_search(30,$a));
echo "<br>";
print_r(count($b));
echo "<br>";

?>

⚫ program for array functions Output:-


<?php

$a=array(10,20,30,40);
$b=array(50,60,"seventy","eighty");

print_r(array_merge($a,$b));
echo "<br>";
print_r(array_combine($a,$b));
echo "<br>";
print_r(array_diff($a,$b));
⚫ wap to demonstrate default
constructor
⚫write any program using if <?php
condition with for loop

<?php class student


{
for($i=0;$i<=10;$i++) public $rollno;
{ public $name;
if($i%2==0) function __construct()
{ {
echo $i; $this->rollno=12;
} $this->name="abc";
} }
function show()
?> {
echo "name is:". $this->name .
Output:- "<br>".
"rollno is:". $this->rollno;
}
}
$s=new student();
$s->show(); Output:-

?>

Output:-

⚫ wap to demonstrate
parametarized constructor

<?php
⚫ demonstrate use of class employee
parametarized constructor
{
public $id;
<?php
public $name;
function __construct($id,$name)
class employee
{
{
$this->id=$id;
public $id;
$this->name=$name;
public $name;
}
function __construct($id,$name)
function show()
{
{
$this->id=$id;
echo "id is:".$this->id."<br>". "name
$this->name=$name;
is:".$this->name;
}
}
function show()
}
{
$e=new employee(123,"abc");
echo "id is:".$this->id."<br>". "name
$e->show();
is:".$this->name;
}
?>
}
$e=new employee(123,"abc");
$e->show();
Output:-
?>
class sub extends multiplication
{
function show4()
{
echo $this->num1 -
$this->num2;
}
}
⚫ wap to implement multilevel $a = new sub();
inheritance
$a->show1();
<?php echo "<br>";
$a->show2();
class addition echo "<br>";
{ $a->show3();
public $num1; echo "<br>";
public $num2; $a->show4();

function show1() ?>


{
$this->num1 = 30; Output:-
$this->num2 = 40;
}

function show2()
{
echo $this->num1 +
$this->num2;
}
}

class multiplication extends addition


{
function show3()
{
echo $this->num1 *
$this->num2;
}
}
⚫ php code for interospection
⚫ wap to implement multiple <?php
inheritance

<?php class demo


{
class a public function disp1()
{ {
public $a=10; echo "this is class demo"."<br>";
public $b=20; }
function show() }
{ class sub extends demo
echo $this->a+$this->b."<br>"; {
} public function disp2()
} {
interface b echo
{ get_class($this)."<br>".get_parent_cla
function show2(); ss($this)."<br>";
} }
class c extends a implements b }
{ if (class_exists("demo"))
function show2() {
{ $d=new demo();
echo $this->a-$this->b; $d->disp1();
} }
} if (class_exists("sub"))
$obj=new c(); {
$obj->show(); $s=new sub();
$obj->show2(); $s->disp2();
}
?>
?>
Output:-
Output:-
⚫ wap to design registration form
<html>
<head>

⚫ wap for serialization <title> Registration Form</title>


<body>
<form method="get"
<?php action="demo.php">
<label> enter name: </label>
$a=serialize(array("this","is","php")); <input type="text" name="t1"
print_r($a."<br>"); id="t"><br/>
$b=unserialize($a); <label> enter mobile no.</label>
print_r($b); <input type="text" name="t2"
id="t1"><br/>
?> <label>enter address:</label>
<textarea name="add" id=t3" cols="50"
Output:- rows="5">
</textarea><br/>
<label>select gender:</label>
<input type="radio" name="g"
value="female">female
<input type="radio" name="g"
value="male">male<br/>
<label>I have read the form</label>
<input type="checkbox" name="m"
value=""><br/>
<input type="submit" name="b"
value="submit"/>
</form>
</body>
</head>
</html>

Output:-
<input type="text" name="t1"

⚫ do validation of webpage
id="t1"><br/>
<label> enter mobile no.</label>
validate.php <input type="text" name="t2"
<?php id="t2"><br/>
if <label>enter address:</label>
($_SERVER['REQUEST_METHOD' <textarea name="t3" id=t3"
] == 'POST') cols="50" rows="5">
{ </textarea><br/>
if (empty($_POST['t1'])) <label>select gender:</label>
{ <input type="radio" name="g"
echo "name can't be value="female">female
blank<br/>"; <input type="radio" name="g"
} value="male">male<br/>
<label>I have read the form</label>
if (!is_numeric($_POST['t3'])) <input type="checkbox" name="m"
{ value=""><br/>
echo "enter correct <input type="submit" name="b"
address<br/>"; value="submit"/>
} </form>
</body>
$p = '/^[1-9]{1}[0-9]{9}$/'; </head>
if (!preg_match($p, </html>
$_POST['t2']))
{ Output:-
echo "enter valid mobile
no<br/>";
}
}
?>

demo4.php
<html>
<head>
<title> Registration Form</title>
<body>
<form method="post"
action="validate.php">
<label> enter name: </label>
Output:-

⚫ wap to check that emails are


⚫ wap to design form using
valid

listbox,combobox,hidden field p12.php


<html>
<html> <body>
<head> <form method="post"
<body> action="validatedata.php">
<form action="" method="post"> <center>
<label>select year</label> Email id:<input type="text"
<select name="s" size="3"> name="email"></br>
<option value="first year">first Email id:<input type="text"
year</option> name="email"></br>
<option value="second Email id:<input type="text"
year">second year</option> name="email"></br>
<option value="third year">third <input type="submit"
year</option> name="submit" value="submit">
</select> </center>
</br> </form>
</br> </body>
<label>select branch</label> </html>
<select name="branch">
<option value="CO">CO</option> Validatedata.php
<option value="IT">IT</option> <?php
<option value="CE">CE</option>
</select> $msg = 0;
<input type="hidden" name="h" $p =
id="h" value="vapm college"/> '/[\w.-]+@[\w.-]+\.[a-zA-Z]{2,6}/';
</form>
</body> if (!preg_match($p,
</html> $_POST["email"]))
{
echo "Enter a valid email ID";
$msg = 1;
}

if ($msg == 0)
{
echo "Data recorded
successfully";
}
?>
⚫ wap to create modify and delete a cookie
<?php

if (!isset($_COOKIE['user']))
{
setcookie('user', 'janhavi', time() + (86400 * 30), "/");
echo "Cookie 'user' is set!<br>";
}
else {
echo "Cookie 'user' is already set. Its value is: " . $_COOKIE['user'] . "<br>";
}

if (isset($_COOKIE['user']))
{

setcookie('user', 'Janvi', time() + (86400 * 30), "/");


echo "Cookie 'user' has been modified!<br>";
}

if (isset($_COOKIE['user']))
{

setcookie('user', '', time() - 3600, "/");


echo "Cookie 'user' has been deleted!<br>";
}
else
{
echo "Cookie 'user' was not set.<br>";
}
?>
⚫wap to start and destroy a session
<?php

session_start();

if (!isset($_SESSION['username']))
{

$_SESSION['username'] = 'Janhavi';
echo "Session started. Username is set to: " . $_SESSION['username'] .
"<br>";
}
else
{

echo "Session already started. Username is: " . $_SESSION['username'] .


"<br>";

session_unset();

session_destroy();
echo "Session destroyed.";
}
?>
⚫ wap to retrieve and present data from database
<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "my_database";

$conn = new mysqli($servername, $username, $password, $dbname);

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

$sql = "SELECT id, name, email FROM users"; // Modify the query according
to your table structure
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// Output data for each row
echo "<table border='1'>";
echo "<tr><th>ID</th><th>Name</th><th>Email</th></tr>"; // Table
headers
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "0 results found";}
$conn->close();
}
⚫ write a php program to insert data into employee table
<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "company";

$conn = new mysqli($servername, $username, $password, $dbname);

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

$sql = "INSERT INTO employee (id, name, position, salary)


VALUES (NULL, 'janhavi', 'Software Developer', 600000)";

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


echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

?>
⚫ write a php program to update table data from student database
<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "school";

$conn = new mysqli($servername, $username, $password, $dbname);

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

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

$student_id = $_POST['student_id'];
$new_name = $_POST['name'];
$new_age = $_POST['age'];
$new_class = $_POST['class'];

$sql = "UPDATE student SET name='$new_name', age=$new_age,


class='$new_class' WHERE student_id=$student_id";

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


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

$conn->close();
?>

<!-- HTML form to input student data for updating -->


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Update Student Data</title>
</head>
<body>

<h2>Update Student Information</h2>


<form method="POST" action="">
<label for="student_id">Student ID:</label>
<input type="text" id="student_id" name="student_id" required><br><br>

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


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

<label for="age">New Age:</label>


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

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


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

<input type="submit" value="Update Student">


</form>

</body>
</html>

You might also like