PHP Practical File 1
PHP Practical File 1
INDEX
S.NO. TOPIC PAGE.NO.
1. Write a program to print a message.
2. Write a program to add two numbers.
3. Write a program to find average of five
numbers.
4. Write a program to find simple interest.
5. Write a program to find an area of triangle.
6. Write a program to find the area of
rectangle.
7. Write a program to find area of circle.
8. Write a program for swapping without
using third variable.
9. Write a program for swapping of numbers.
10. Write a program to display local variable.
11. Write a program to display global variable.
12. Write a program to access the global
variable inside the function.
13. Write a program to display a static
variable.
14. Write a program of constant.
CASE-SENSITIVE CONSTANT.
15. Write a program of constant
CASE-INSENSITIVE CONSTANT.
16. Write a program of arithmetic operator.
17. Write a program of increment and
decrement operator.
18. Write a program of comparison operator.
19. Write a program of assignment operator.
20. Write a program of logical operator.
21. Write a program of conditional operator.
22. Write a program of if statement.
23. Write a program of if-else statement.
3
Output:
7
PROGRAM:
<?php
echo "<font
size=50><center><
font color=indigo>
Welcome to php";
?>
8
Output:
9
PROGRAM:
<?php
$num1 = 10;
$num2 = 20;
?>
10
Output:
11
<?php
$a=50;
$b=60;
$c=70;
$d=50;
$e=65;
$f=$a+$b+$c+$d+$e;
$e=$f/5;
?>
12
Output:
13
<?php
$principal = 1820;
$rate = 6;
$time = 8;
?>
14
Output:
15
<?php
// Area Of Triangle
$base = 10;
$height = 15;
?>
16
Output:
17
//Area Of Rectangle
$length =14;
$width =12;
?>
18
Output:
19
<?php
//Area of circle
$r=4;
$pi=3.14;
$a=$pi*$r*$r;
?>
20
Output:
21
PROGRAM:
<?php
$num1 = 10;
$num2 = 20;
Before Swap";
After Swap";
?>
22
Output:
23
<?php
$y=60;
$third=$x;
$y=$third;
?>
24
Output:
25
PROGRAM:
<?php
function display()
display();
?>
26
Output:
27
<?php
function Test ()
echo "<br>";
Variable y is:".$y;
Test ();
Variable x is :".$x;
echo"<br>";
?>
28
Output:
29
PROGRAM:
<?php
$a = 10; // Global variable
function display ()
{
global $a;
echo "<font color=red><center><font size=50> <br>
Accessing value Inside function = $a";
}
display ();
echo "<font color=blue><center><font size=50> <br>
Accessing value outside function = $a";
?>
30
Output:
31
<?php
function displayStaticVariable()
static $count = 0;
$count++;
displayStaticVariable();
displayStaticVariable();
displayStaticVariable();
displayStaticVariable();
displayStaticVariable();
?>
32
Output:
33
PROGRAM:
<?php
echo GREETING;
echo"<br>";
echo greeting;
?>
34
Output:
35
PROGRAM:
<?php
define("GREETING","<br><center><font
color=blue><font size=10>Welcome you all", true);
echo GREETING;
echo"<br>";
echo greeting;
?>
36
Output:
37
<?php
$a = 20;
$b = 10;
?>
38
Output:
39
PROGRAM:
<?php
$x = 50;
$y = $x++;
// Post-increment
$z = ++$x; //Pre-increment
// Post-decrement
$v = $x--;
// Pre-decrement
$q = --$x;
?>
40
Output:
41
} else {
echo "Not Equal (!=): ";
echo "<font color='black'>False<br></font>";
if ($a != $b)
}
{
echo "Less than or equal to (<=): ";
echo "<font color='blue'>True<br></font>";
if ($a <= $b) {
}
echo "<font color='blue'>True<br></font>";
else
} else {
{
echo "<font color='black'>False<br></font>";
echo "<font color='black'>False<br></font>";
}
}
?>
42
Output:
43
<?php
//Assignment operator
$x = 10;
echo $x . "<br>";
$x = 20;
$x += 30;
echo $x . "<br>";
$x = 50;
$x -= 20;
echo $x . "<br>";
$x = 5;
$x *= 25;
echo $x . "<br>";
$x = 50;
$x /= 10;
echo $x . "<br>";
$x = 100;
$x %= 15;
echo $x . "<br>";
?>
44
Output:
45
{ else
if ($a || $b)
} else {
if (!$a)
} else
}
46
Output:
47
//conditional operator
$a=10;
$b=20;
?>
48
Output:
49
PROGRAM:
<?php
// IF STATEMENT
$age = 24;
if ($age>18)
?>
50
Output:
51
<?php
// IF-ELSE STATEMENT
$age = 10;
if ($age>18)
else
You are below age, so you are not in the list of voter"
?>
52
Output:
53
<?php
$marks = 250;
else
?>
Output:
54
PROGRAM:
Today is Thursday";
break;
Output:
56
57
PROGRAM:
<?php
$number = 2;
$number * $i = $product<br>";
?>
58
Output:
59
PROGRAM:
<?php
$sum = 0;
$number = 2;
$sum += $number;
$number += 2;
?>
60
Output:
PROGRAM:
<?php
$number = 1;
do
$number <br>";
$number++;
?>
Output:
62
PROGRAM:
<?php
$cars=array("BMW","Jaguar","Bugatti","Ferrari");
foreach($cars as $value)
$value<br>";
?>
64
Output:
65
PROGRAM:
<?php
?>
66
Output:
67
PROGRAM:
<?php
$num = 5;
$factorial = 1;
?>
68
Output:
69
PROGRAM:
<?php
$number=5678;
if ($number%2==0)
else
?>
70
Output:
71
PROGRAM:
<?php
$num = 407;
$total = 0;
$x = $num;
while($x != 0)
$rem= $x % 10;
if($num == $total)
else
?>
72
Output:
73
PROGRAM:
<?php
$number =0;
$a1=0;
$a2=1;
echo "\n";
while ($number<10)
$a3=$a2 + $a1;
$a1=$a2;
$a2=$a3;
$number=$number+1;
?>
74
Output:
75
<?php
echo "*";
echo "<br>";
echo "*";
echo "<br>";
?>
76
Output:
77
$rows = 5;
echo "*";
echo "<br>";
?>
78
Output:
79
PROGRAM:
<?php <?php
echo"<br><font color=orange>"."Welcome";
Hello World! <br>
?>
Welcome to php";
?>
80
Output:
81
PROGRAM:
<?php
<?php
require("Add.php");
$num1 = 10;
?>
$sum = $num1 + $num2;
?>
82
Output:
83
PROGRAM:
<?php
Hello,Welcome To Php";
?>
84
Output:
85
PROGRAM:
<?php
return $sum;
?>
Output:
86
87
<?php
$num1.<br/>";
ABC(303);
ABC();
?>
Output:
88
89
<?php
Hello<br/>";
$message = "ab";
$message();
?>
Output:
90
91
PROGRAM:
<?php
// Call By Value
function abc($x)
$x=$x+10;
return($x);
$a=20;
echo abc($a)."<br>";
echo($a);
?>
92
Output:
93
<?php
// Call By Reference
function abc(&$x)
$x=$x+10;
return($x);
$a=20;
echo abc($a)."<br>";
echo($a);
?>
94
Output:
95
PROGRAM:
<?php
function display($number)
if($number<=5)
$number <br/>";
display($number+1);
display(1);
?>
96
Output:
PROGRAM:
<?php
$number = array(1,2,3,4,5);
foreach($number as $value)
97
Output:
Output:
98
PROGRAM:
<?php
$subject=array(
$subject['English']="Low";
$subject['Hindi']="High";
$subject['Maths']="Medium";
?>
Output:
100
PROGRAM:
<?php
// Multidimensional array
$marks = array (
echo $marks['Amit']['English']."<br/>";
echo $marks['Aman']['Hindi']."<br/>";
echo $marks['Ajay']['Maths']."<br/>";
?> Output:
102
PROGRAM:
103
<?php
$cars=array("BMW","Audi","Ferrari");
$arrlength = count($cars);
for($x=0;$x<$arrlength;$x++)
echo$cars[$x];
echo"<br>";
?>
Output:
104
PROGRAM:
<?php
echo $cars[$x];
echo "<br>";
?>
Output:
106
107
PROGRAM:
<?php
echo $str;
?>
Output:
108
DOUBLE QUOTED:
PROGRAM:
109
<?php
$num1 = 10;
text within
?>
Output:
110
PROGRAM:
111
<?php
$drink = 'coffee';
?>
Output:
112
PROGRAM:
<?php
$msg='ALL IS WELL';
for($index=0;$index<11;$index++)
$ch=$msg{$index};
?>
Output:
114
PROGRAM:
<?php
// Concatenation Operator(.)
$string1 = "TISHITA";
$string2 = "MATHUR";
?>
Output:
116
CONCATENATING ASSIGNMENT
OPERATOR (.=)
117
PROGRAM:
<?php
$string1 = "Tishita";
$string2 = "Mathur";
echo"<br><font color=red>";
$string1 .= $string2;
echo $string1;
?>
Output:
118
PROGRAM:
echo "Without Rtrim: $str1 <font color=blue><br/>"; echo wordwrap($str3, 15, "<br>")."<font
color=blue><br/>"; // Wraps text at 15
echo "With Rtrim: ".rtrim($str1)."<font
characters
color=red><br/>";
?>
120
Output:
121
<?php
$var = "variables";
$msg = <<<BCA
<br>
Good
BCA;
$msg";
?>
Output:
122
123
PROGRAM:
<html>
<body>
<form name="f1">
<br><br>
</form>
</body>
</html>
Output:
124
PROGRAM:
<html>
<body>
<form name="f1">
FEEDBACK:<br><b>
</textarea>
</form>
</body>
</html>
Output:
126
PROGRAM:
<html>
<body>
<form name="f1">
127
Output:
128
PROGRAM:
<html>
<body>
<form name="f1">
</label><br>
</label><br>
</form>
</body>
</html>
Output:
130
PROGRAM:
<html>
<body>
<form name="f1">
<select name="cars">
<option value="vol">Volvo</option>
<option value="vol">BMW</option>
<option value="vol">Audi</option>
<option value="vol">Mercedes</option>
</select>
</form>
</body>
</html>
Output:
132
133
PROGRAM:
<html>
<body>
<form method="post">
</form>
<?php
if (isset($_POST['redirect_button']))
header("Location: https://www.google.com");
exit;
?>
</body>
</html>
134
Output:
135
$a = 30;
$y = 50;
function multiplication()
$GLOBALS['z']=$GLOBALS['a']*$GLOBALS['y'];
multiplication();
echo"<font color=blue>".$z;
?>
136
Output:
137
$_SERVER []
PROGRAM:
<?php
echo "<br>";
echo "<br>";
echo "<br>";
echo "<br>"
?>
138
Output:
139
$_REQUEST []
PROGRAM:
<html>
<body>
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_REQUEST['fname'];
if (empty($name))
Else
?>
</body>
140
Output:
141
<head>
</head>
<body>
<h2>Admission Form</h2>
<option value="BCA">BCA</option>
<option value="BBA">BBA</option>
<option value="BA">BA</option>
</form>
</body>
</html>
142
Output:
143
PROGRAM:
<?php
setcookie("name","Khushi",time()+3600,"/","",0);
setcookie("age","20", time()+3600,"/","",0);
?>
<html>
<head>
</head>
<body>
</body>
</html>
144
Output:
145
PROGRAM:
<?php
setcookie("name","Tishita Mathur",time()+3600,"/","",0);
setcookie("age","20", time()+3600,"/","",0);
?>
<html>
<head>
</head>
<body>
<?php
?>
146
Output:
147
PROGRAM:
<?php
?>
<html>
<head>
</head>
<body>
</body>
</html>
148
Output:
149
PROGRAM:
<?php
setcookie("name", "Tishita");
?>
<html>
<head>
</head>
<body>
</body>
</html>
150
Output:
151
<?php
session_start ( );
else
?>
<html>
</head>
<body>
</body>
</html>
152
Output:
153
PROGRAM:
<?php <?php
session_start ( ); session_start ( );
?> ?>
<html> <html>
<body> <body>
<?php <?php
$_SESSION["username"] = "Tishita"; echo "User is: " .$_SESSION
echo "<center>Session information is set ["username"];
successfully.<br/>"; ?>
?> </body>
<a href="session2.php">Visit next page</a> </html>
</body>
</html>
Output:
154
PROGRAM:
<?php
session_start();
if (empty($_SESSION['count']))
$_SESSION['count'] = 1;
else
$_SESSION ['count']++;
?>
<p>
Hello,you have seen this page <?php echo $_SESSION['count']; ?> times.
</p>
<p>
To Continue,
</p>
156
Output:
157
PROGRAM:
<?php
session_start();
$_SESSION["city"] = "Mohali";
$enc_session = session_encode();
print $enc_session."<br/><br/>";
$_SESSION["city"] = "Chandigarh";
// Printing $_SESSION
print "<pre>";
print_r($_SESSION);
print "</pre>";
session_decode ($enc_session);
print "<pre>";
print_r($_SESSION);
print "</pre>";
?>
158