php-so
php-so
PHP Solved
Practical-1
AIM: - Create a php webpage and print “hello world”.
<Html>
<Head>
<Title> My Simple Program </Title>
</head>
<Body>
<?php
echo "Hello World"
?>
</Body>
</Html>
O/P:
Hello World
Practical-2
AIM: - Create a php program to find odd or even number from given
number.
<Html>
<Head>
<Title> Find out odd or even number. . ! </title>
</Head >
<body>
<?php
$a = 10;
if ($a % 2==0)
{
echo "Given number is" . "<br>" . "<b>EVEN<b>" ;
}
else
{
echo "Given number is" . "<br>" . "<b>ODD<b>";
}
?>
</body> </html>
O/P:
Given number is
EVEN
Downloaded by Yogeshvari Kalskar ([email protected])
2
lOMoARcPSD|37203747
Practical-3
AIM: - Write a php program to find maximum of three numbers.
<html>
<head>
<title> Max out of three </title>
</head>
<body>
<?php
$a = 1;
$b = 4;
$c = 3;
if($a > $b)
{
if($a > $c)
echo "Maximum num a = $a";
else
echo "Maximum num c = $c";
}
else
{
if($c > $b)
echo "Maximum num c = $c";
else
echo "Maximum num b = $b";
}
?>
</body>
</html>
O/P:
Maximum num b =4
3
lOMoARcPSD|37203747
Practical-4
AIM: - Write a PHP program to swap two numbers.
<html>
<head>
<title>
Swapping of two numbers. . !
</title>
</head>
<Body>
<?php
$a = 10;
$b = 20;
echo "a = $a"."<br>"."b = $b"."<br>";
$a = $a + $b;
$b = $a - $b;
$a = $a - $b;
echo "<b>After Swapping"."<br>"." a = $a"."<br>"."b = $b<b>";
?>
</body>
</html>
O/P:
a =10
b =20
After Swapping
a =20
b =10
4
lOMoARcPSD|37203747
Practical-5
5.1 AIM: - Write a PHP Program to demonstrate the variable function:
Gettype():
<html>
<head>
<title>
Variable function:gettype:1
</title>
</head>
<body>
<?php
$no = 5;
$value = 13.5;
$name = "john";
$var = true;
$myarray= array(110,45," John",1.5,true);
echo gettype($no)."<br/>";
echo gettype($value)."<br/>";
echo gettype($name)."<br/>";
echo gettype($var)."<br/>";
echo gettype($myarray)."<br/>";
?>
</body>
</html>
O/P:
integer
double
string
boolean
array
5
lOMoARcPSD|37203747
<html>
<head>
<title> Variable function:gettype:2 </title>
</head>
<body>
<?php
$data = array(1,1.5,null,"John",new stdclass,true);
foreach($data as $value)
{
echo gettype($value)."<br/>";
}
?>
</body>
</html>
O/P:
integer
double
NULL
string
object
boolean
6
lOMoARcPSD|37203747
Practical-6
AIM: Write a PHP Program to demonstrate the variable function:
Settype()
<html>
<head>
<title> Variable function:Settype:1 </title>
</head>
<body>
<?php
$var1 ="anil";
$var3 = "1Bipin";
$var4 = "anil1";
$var2 = true;
settype($var1, "integer");
settype($var2, "string");
settype($var3, "integer");
settype($var4, "integer");
echo gettype($var1)."</br>";
echo gettype($var2)."</br>";
echo gettype($var3)."</br>";
echo gettype($var4)."</br>";
echo $var1."</br>";
echo $var2."</br>";
echo $var3."</br>";
echo $var4."</br>";
?>
</body>
</html>
O/P:
integer
string
integer
integer
0
1
1
0 Downloaded by Yogeshvari Kalskar ([email protected])
7
lOMoARcPSD|37203747
Practical-7
7.1 AIM: Write a PHP Program to demonstrate the variable function:
isset()
<html>
<head>
<title> Variable function:isset:1 </title>
</head>
<body>
<?php
$no = 9;
$name = "John";
$val=10.5;
echo isset($no)."</br>";
echo isset($name)."</br>";
echo isset($val)."</br>";
echo $no."</br>";
echo $name."</br>";
echo $val."</br>";
?>
</body>
</html>
O/P:
1
1
1
9
John
10.5
8
7.2 Write a PHP Program to demonstrate the variable function:
lOMoARcPSD|37203747
isset()
<html>
<head>
<title>
Variable function:isset:2
</title>
</head>
<body>
<?php
$data = array('$no'=>9,'$name'=>"John",'$marks'=>10.5)
foreach($data as $val1)
{
echo $val1."<br/>";
}
foreach($data as $val2)
{
echo isset($val2)."<br/>";
}
?>
</body>
</html>
O/P:
9
John
10.5
1
1
1
9
lOMoARcPSD|37203747
Practical-8
8.1 AIM: Write a PHP Program to demonstrate the variable function:
unset()
<html>
<head>
<title> Variable function:unset:1 </title>
</head>
<body>
<?php function unset_val1()
{
global $val1;
echo $val1;
unset($val1);
}
$val1 = "John";
unset_val1();
?>
</body>
</html>
O/P:
John
10
lOMoARcPSD|37203747
O/P:
John
( ! ) Notice: Undefined variable: val1 in C:\wamp\www\@nil_09\Pr-8\unset2.php
on line 19
Call Stack
# Time Memory Function Location
1 0.0007 365344 {main}( ) ..\unset2.php:0
O/P:
Before unset(): 1,After unset(): 55
Before unset(): 2,After unset(): 55
Before unset(): 3,After unset(): 55
11
lOMoARcPSD|37203747
Practical-9
AIM:- Give the example of variable function:strval()
<html>
<head>
<title>
Variable Function:Strval()
</title>
</head>
<body>
<?php
$val1 = 22.110;
echo strval($val1);
?>
</body>
</html>
O/P:
22.11
<html>
<head>
<title>
Variable Function:Floatval()
</title>
</head>
<body>
<?php
$val1 = "3.14pie";
echo floatval($val1);
?>
</body>
</html>
O/P:
3.14
12
lOMoARcPSD|37203747
<body>
<?php
echo intval(102).'<br>';
echo intval(102.22).'<br>';
echo intval('102').'<br>';
echo intval(+102).'<br>';
echo intval(-102).'<br>';
echo intval(0102).'<br>';
echo intval('0002').'<br>';
echo intval(1e20).'<br>';
echo intval(0x1B).'<br>';
echo intval(10200000).'<br>';
echo intval(10200000000000000000).'<br>';
echo intval(10,2).'<br>';
echo intval('10',2).'<br>';
?>
</body>
</html>
O/P:
102
102
102
102
-102
66
2
0
27
10200000
0
10
2 Downloaded by Yogeshvari Kalskar ([email protected])
13
lOMoARcPSD|37203747
Practical-10
AIM: - Give the example of variable function: print_r()
<html>
<head>
<title>
Variable Function:print_r()
</title>
</head>
<body>
<pre>
<?php
$data = array('e_no'=>106680307009,'e_name'=>'John
R .Cena','S.P.I'=>array('1st'=>7.75,'2nd'=>8.27,'3rd'=>8.20,'4th'=>7.57));
print_r ($data);
?>
</pre>
</body>
</html>
O/P:
Array
(
[e_no] => 106680307009
[e_name] => John R .Cena
[S.P.I] => Array
(
[1st] => 7.75
[2nd] => 8.27
[3rd] => 8.2
[4th] => 7.57
)
14
lOMoARcPSD|37203747
<html>
<head>
<title>
Variable Function:var_dump()
</title>
</head>
<body>
<?php
$val1 = 678;
$val2 = "a678";
$val3 = "678";
$val4 = "BetterThanLike.com";
$val5 = 698.99;
$val6 = +125689.66;
echo var_dump($val1)."<br>";
echo var_dump($val2)."<br>";
echo var_dump($val3)."<br>";
echo var_dump($val4)."<br>";
echo var_dump($val5)."<br>";
echo var_dump($val6)."<br>";
?>
</body>
</html>
O/P:
int 678
float 698.99
float 125689.66
Downloaded by Yogeshvari Kalskar ([email protected])
15
lOMoARcPSD|37203747
Practical-11
AIM: - Give the example of string function: substr():
<html>
<head>
<title> String Function:Substr():1 </title>
</head>
<body>
<b>If the start is non-negative, the returned string will start at the start'th position
in string, start from 0.</b><br/><br/>
<?php
echo "Substring with positive start:".substr("abcdef",2)."<br/>";?><br/>
<b>If the start is negative, the returned string will start at the start'th character in
string, from the end of the string.</b><br/><br/>
<?php
echo "Substring with negative start:".substr("abcdef",-2)."<br/>";?><br/>
<b>If the start is less than or equal to start characters long, false will
return</b><br/><br/>
<?php
echo "start is <= string".substr("abcdef",7)."<br/><br/>";
echo "Finish";
?>
</body>
</html>
O/P:
If the start is non-negative, the returned string will start at the start'th position in
string, start from 0.
If the start is negative, the returned string will start at the start'th character in
string, from the end of the string.
If the start is less than or equal to start characters long, false will return
start is <= string
Finish Downloaded by Yogeshvari Kalskar ([email protected])
16
lOMoARcPSD|37203747
<?php
echo "Substring with positive length:".substr("NBPATELPILUDARA",2,4)."<br/>";
?></br>
<?php
echo "Substring with positive length:".substr("abcdef",-3,2)."<br/>"; ?></br>
<?php
echo "Substring with positive length:".substr("NBPATELPILUDARA",-
5,2)."<br/>";?></br>
<b>If the length is negative, the returned string will start at the start'th position in
string AND count the length.</b><br/><br/>
<?php
echo "Substring with negative length:".substr("abcdef",2,-2)."<br/>";?></br>
<?php
echo "Substring with negative length:".substr("NBPATELPILUDARA",2,-
4)."<br/>";?></br>
<?php
echo "Substring with negative length:".substr("abcdef",-2,-2)."<br/>";?></br>
<?php
echo "Substring with negative length:".substr("NBPATELPILUDARA",-2,-
4)."<br/>";?></br>
<?php
echo "Substring with negative length:".substr("NBPATELPILUDARA",-4,-
2)."<br/>";?></br>
<?php
echo "Substring with negative length:".substr("NBPATELPILUDARA",-5,-
4)."<br/>";?></br>
</body> </html> Downloaded by Yogeshvari Kalskar ([email protected])
17
lOMoARcPSD|37203747
O/P:
If the length is non-negative, the returned string will start at the start'th position in
string AND count the length.
If the length is negative, the returned string will start at the start'th position in
string AND count the length.
18
lOMoARcPSD|37203747
<<html>
<head>
<title> String Function: Substr():3 </title>
</head>
<body>
<?php
echo substr("John R .Cena",0,1)."<br/>";
O/P:
J
ohn
R
19
lOMoARcPSD|37203747
Practical-12
AIM: - Give the example of string function: strcmp()
<html>
<head> <title> String Function:strcmp() </title> </head>
<body>
<?php
$str1 = 'a';
$str2 = 'b';
echo strcmp($str1, $str2)."<br/>";
$str3 = 'b';
$str4 = 'a';
echo strcmp($str3, $str4)."<br/>";
$str5 = "Anil";
$str6 = "anil";
echo strcmp($str5, $str6)."<br/>";
?>
</body></html>
O/P:
-1
1
-1
O/P:
0
0
Downloaded by Yogeshvari Kalskar ([email protected])
20
lOMoARcPSD|37203747
Practical-13
AIM: - Give the example of string function: strpos():1
<html>
<head> <title>String Function:strpos():1</title> </head>
<body>
<?php
$string = "I am anil";
$pos = strpos($string,"nil");
if($pos === false)
{
echo "Not found.";
}
Else { echo "Found..!"; }
?>
</body> </html>
O/P:
Found..!
21
lOMoARcPSD|37203747
<html>
<head>
<title>
String Function:strpos():3
</title>
</head>
<body>
<?php
$string = "I am anil";
$pos = strpos($string,"a",3);
if($pos === false)
{
echo "Not found.";
}
else
{
echo "Found at $pos. .!";
}
?>
</body>
</html>
O/P:
Found at 5. .!
22
lOMoARcPSD|37203747
Practical-14
AIM: - Write a PHP program that demonstrate form element(input
elements).
<html>
<head>
<title>general form</title>
</head>
<body>
<form action="handler.php" method="get">
Name: <input type = "text" name = "name"> <br> <br>
Reg. No.: <input type = "text" name = "reg_no"> <br> <br>
Hobby:<br>
<input type = "checkbox" name = "hobbies" value = "sport">
Reading <br/>
<input type = "checkbox" name = "hobbies" value = "sport">
Playing games <br/>
<input type = "checkbox" name = "hobbies" value = "sport">
internet surffing <br/> <br>
Gender:
<input type = "radio" name = "Gender" value = "Male"> Male
<input type = "radio" name = "Gender" value = "Female">
Female <br> <br>
Branch:
<select name = "Branches">
<option>C.E.</option>
<option>Mech</option>
<option>E.C.</option>
<option>I.T.</option>
<option>E.E.</option>
</select> <br/> <br>
<input type = "Submit" value = "Save">
<input type = "Reset" value = "Cancle">
</form>
</body>
</html>
23
lOMoARcPSD|37203747
Practical-15
AIM: - Write a PHP program that demonstrate passing variable using URL.
file name : handler.php
<html>
<head>
<title>Query String</
title> </head>
<body>
<form>
Hello Friends . . !<br>
I am
<?php
echo $_GET["name"]; ;
?>
</form>
</body>
</html>
O/P:
Hello Friends. . . !
I am John Cena
25
lOMoARcPSD|37203747
Practical-16
AIM: - Write a PHP program that demonstrate use of session:1
Note: create following two PHP files. Acess(Run) ne ̀ after ne ̀ i ȁ̀ localhost
create_session.php
<html>
<head>
<title>Session</title>
</head> <body>
<?php
session_start();
$_SESSION["name"]="Anil";
$_SESSION["Password"]="Abc@123";
?>
</body>
</html>
session_result.php
<html>
<head>
<title>Session</title>
</head>
<body>
<?php
session_start();
echo "Welcome ".$_SESSION["name"]."<br/>";
echo "your Password:".$_SESSION["Password"];
?>
</body>
</html>
O/P:
Welcome Anil
your Password: Abc@123
26
lOMoARcPSD|37203747
Practical-17
AIM: - Write a program that demonstrate use of cookies: 1
Note: create following two PHP files. Acess(Run) one after one via localhost
create_cookie.php
<html>
<head>
<title>Examle of cookies. . !</title>
</head>
<body>
<?php
setcookie("name","John Cena",time()+3600,"/","",0);
setcookie("age","21",time()+3600,"/","",0);
?>
</body>
</html>
cookie_result.php
<html>
<head>
<title>Access cookies. . !</title>
</head>
<body>
<?php
echo $_COOKIE["name"]."<br/>";
echo $_COOKIE["age"]."<br/>";
?>
</body>
</html>
O/P:
John Cena
21
27
lOMoARcPSD|37203747
Practical-18
AIM: - Write a PHP program to create a database using MySQL.
<html>
<head>
<title>Create Database. </title>
</head>
<body>
<?php
$con = mysqli_connect("localhost","root","");
if(!$con)
{
die("connection error");
}
echo "Connection opened"."</
br>";
$query = "create database student";
$crdb = mysqli_query($con, $query,$con);
if(!$crdb)
{
die("not created. .!".mysqli_error());
}
echo "database created.. !";
?>
</body>
</html>
O/P:
Connection open
database created.. !
28
lOMoARcPSD|37203747
<html>
<head>
<title>Drop Database. </title>
</head>
<body>
<?php
$con = mysqli_connect("localhost","root","");
if(!$con)
{
die("connection error");
}
echo "Connection open"."</br>";
O/P:
Connection open
database droped.. !
29
lOMoARcPSD|37203747
O/P:
Connection open
Database is selected
table created.. !
Downloaded by Yogeshvari Kalskar ([email protected])
30
lOMoARcPSD|37203747
AIM: - Write a PHP program to insert record into a table using MySQL.
<html>
<head>
<title>ŅnVert into Database. </title>
</head>
<body>
<?php
$con = mysqli_connect("localhost","root","");
if(!$con) {
die("not opened");
}
echo "Connection open"."</br>";
$db = mysqli_select_db($con, "student");
if(!$db) {
die("Database not found".mysqli_error());
}
echo "Database is selected"."</br>";
$query = "insert into computer values(7009,'Anil J Basantani','Sadhana colony
Jamnagar')";
$insrtb = mysqli_query($con, $query);
if(!$insrtb){
die("Record not inserted.".mysqli_error());
}
echo "Record inserted successfully. . .!"."</br>";
?>
</body>
</html>
O/P:
Connection open
Database is selected
Record inserted successfully. . .!
31
lOMoARcPSD|37203747
<html>
<head>
<title>Create Database. </title>
</head>
<body>
<?php
$con = mysqli_connect("localhost","root","");
if(!$con)
{
die("not opened");
}
echo "Connection open"."</br>";
?>
</body>
</html>
O/P:
Connection open
Database is selected
table droped.. !
32
lOMoARcPSD|37203747
<html>
<head>
<title>Create Database. </title>
</head>
<body>
<?php
$con = mysqli_connect("localhost","root","");
if(!$con)
{
die("not opened");
}
echo "Connection open"."</br>";
?>
</body>
</html>
O/P:
Connection open
Database is selected
table updated.. !
Downloaded by Yogeshvari Kalskar ([email protected])
33
lOMoARcPSD|37203747
AIM:- Write a PHP program to select data and show into table format.
<html>
<head>
<title>Create Database. </title>
</head>
<body>
<?php
$con = mysqli_connect("localhost","root","");
if(!$con)
{
die("connection error");
}
echo "Connection open"."</br>";
34
echo "</table>";
lOMoARcPSD|37203747
?>
</body>
</html>
O/P:
Connection open
Database is selected
ID Name Branch
9 Anil J Basantani CE
9 Anil J Basantani CE
9 Anil J Basantani CE
Practical-19
AIM: - Create a student Registration in PHP and Save and Display the
student Records.
<html>
<head>
<title>Registration Form</title>
</head>
<body>
<form action = "<?php $_PHP_SELF ?>" method = "POST">
Name:
<input type = "text" name = "name">
<br><br>
Reg no.:
<input type = "text" name = "reg_no">
<br><br>
Gender:
<input type = "text" name = "gender">
<br><br>
Downloaded by Yogeshvari Kalskar ([email protected])
35
lOMoARcPSD|37203747
Address:
<textarea name = "address" type = "textarea"></textarea>
<br><br>
mysqli_close($con);
}
}
?>
Downloaded by Yogeshvari Kalskar ([email protected])
37
lOMoARcPSD|37203747
Extra-1
AIM: - Write a program to Develop student registration form and display
all the submitted data on another page.
Form fill.html
<html>
<head>
<title>insert data in form</title>
</head>
<body>
<form action = "getdata.php" method = "POST">
Name:
<input type = "text" name = "name">
<br><br>
Reg No.:
<input type = "text" name = "reg_no">
<br><br>
Address:
<textarea name = "address" type = "textarea"></
textarea> <br><br>
Contyact No:
<input type = "text" name = "contact_no">
<br><br>
Email ID:
<input type = "text" name = "email_id"> <br><br>
38
Getdata.php
lOMoARcPSD|37203747
<html>
<head>
<title>get data from another page</title>
</head>
<body>
<?php
</body>
</html>
Exercise :
Write code to store the data in student table
39
lOMoARcPSD|37203747
Extra-2
AIM: - Write a program to read customer information like c_no, c_name,
item_purchased and mob_no from customer table and display all this
information in table format on output screen.
HINT: create a databse db_customer and create a table customer & insert some data
<html>
<head>
<title>display data in table format</title>
</head>
<body>
<?php
$con = mysqli_connect("localhost","root","");
if(!$con)
{
die("connection error".mysqli_error());
}
echo "Connection open"."<br/>";
$sldb = mysqli_select_db("coust",$con);
if(!$sldb)
{
die("not found".mysql_error());
}
echo "Database selected"."<br/>";
$query = "select * from customer";
$sql = mysql_query($query);
40
echo "</tr>";
lOMoARcPSD|37203747
}
echo "</table>";
?>
</body>
</html>
O/P:
Connection open
Database selected
C_No C_Name Item_Purchased Mob_no
1 Anil Book 2147483647
2 Yogesh Marker 2147483647
41
lOMoARcPSD|37203747
Extra-4
AIM: - Write a program that keeps track of how many times a visitor has
loaded the page.
<html>
<head>
<title>php counter</title>
</head>
<body>
<?php
session_start();
if(isset($_SESSION['counter']))
{
$_SESSION['counter'] += 1;
}
else
{
$_SESSION['counter'] = 1;
}
echo "You have visited this page ".$_SESSION['counter']." time in this session";
?>
</body>
</html>
O/P:
You have visited this page 1 time in this session
43
lOMoARcPSD|37203747
Extra-5
AIM: Write a program that displays a different message based on time of
day. For example page should display “Good Morning” if it is
accessed in the morning.
<?PHP
//Change the messages to what you want.
$afternoon = "Good afternoon! ";
$evening = "Good evening! ";
$late = "Working late? ";
$morning = "Good morning! ";
$friday= "Get ready for the weekend! ";
//Get the current hour
$current_time = date('G');
?>
O/P :-
Good morning! (Its base on Current Time)
44