INDEX PHP
INDEX PHP
INDEX
S.NO. TOPIC PAGE.NO.
1. Write a program to print a message. 6-7
2. Write a program to add two numbers. 6-7
3. Write a program to find average of five 8-9
numbers.
4. Write a program to find simple interest. 8-9
5. Write a program to find an area of triangle. 10-11
6. Write a program to find the area of 10-11
rectangle.
7. Write a program to find area of circle. 12-13
8. Write a program to display a static 12-13
variable.
9. Write a program for swapping without 14-15
using third variable.
10. Write a program for swapping of numbers. 16-17
11. Write a program to display local variable. 18-19
12. Write a program to access the global 18-19
variable inside the function.
13. Write a program to display global variable. 20-21
14. Write a program of constant. 22-23
CASE-SENSITIVE CONSTANT.
15. Write a program of constant 22-23
CASE-INSENSITIVE CONSTANT.
16. Write a program of arithmetic operator. 24-25
17. Write a program of increment and 26-27
decrement operator.
18. Write a program of comparison operator. 28-29
19. Write a program of assignment operator. 30-31
20. Write a program of logical operator. 32-33
21. Write a program of conditional operator. 34-35
22. Write a program of if statement. 34-35
23. Write a program of if-else statement. 36-37
24. Write a program of if-elseif-else statement. 38-39
3
Output:
Output:
7
Welcome to php";
?>
?>
8
Output:
Output:
9
$e=65;
$f=$a+$b+$c+$d+$e;
echo "<font color=orange><center><font size=50>
Sum of five numbers is: ".$f."\n";
$e=$f/5;
PROGRAM:
<?php
$principal = 1820;
$rate = 6;
$time = 8;
$interest = ($principal * $rate * $time) / 100;
echo "<font color=orange><center><font size=50>
Simple Interest: $interest";
?>
10
Output:
Output:
11
//Area Of Rectangle
$length =14;
$width =12;
echo"<center><font color=navy blue><font size=50>
Area Of Rectangle is $length * $width=".($length *$width)."<br>";
?>
12
Output:
Output:
13
displayStaticVariable();
displayStaticVariable();
?>
14
Output:
15
PROGRAM:
<?php
$num1 = 10;
$num2 = 20;
Before Swap";
After Swap";
?>
16
Output:
17
PROGRAM:
<?php
$y=60;
$y=$third;
echo"<br><br><font size=30><font color=purple>After Swapping
:<br></font color></font size>";
echo"<font size=20> x =".$x. " y=".$y;
?>
18
Output:
Output:
19
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>
Output:
21
function Test ()
echo "<br>";
?>
22
Output:
Output:
23
PROGRAM:
<?php
//define a Case - Sensitive Constant
?>
PROGRAM:
<?php
//define a case-insensitive constant
define("GREETING","<br><center><font color=blue><font size=10>Welcome you all",
true);
echo GREETING;
echo"<br>";
//will also output the value of the constant
echo greeting;
?>
24
Output:
25
PROGRAM:
<?php
$a = 20;
$b = 10;
echo"<b><center><font size = 10><font color = blue>";
echo "Addition : " . ($a + $b) ."<br>";
echo"<b><center><font size = 10><font color = orange>";
echo"Subtraction :" .($a - $b) ."<br>";
Output:
27
PROGRAM:
<?php
$x = 50;
$z = ++$x; //Pre-increment
echo "<font color=blue><center><b>Value of z: $z <br>";
echo "<font color=red><center>Value of x after pre-increment: $x <br>";
// Post-decrement
$v = $x--;
Output:
29
echo "Not Equal (!=): "; echo "Greater than or equal to (>=): ";
{ echo "<font
color='blue'>True<br></font>";
echo "<font
color='blue'>True<br></font>"; } else {
} echo "<font
color='black'>False<br></font>";
else
} echo "Less than or equal to (<=): ";
{
if ($a <= $b) { echo "<font
echo "<font color='blue'>True<br></font>";
color='black'>False<br></font>";
} else { echo "<font
} color='black'>False<br></font>"; }
// Additional comparison examples ?>
echo "Greater than (>): ";
if ($a > $b) {
30
Output:
31
//Assignment operator
echo"<b><center><font color=blue><font size=50>";
$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>";
?>
32
Output:
33
{ else
echo "<font {
color='orange'>True<br></font>";
echo "<font
} else { color='purple'>False<br></font>";
echo "<font }
color='purple'>False<br></font>";
?>
}
// OR (||) Operator
if ($a || $b)
} else {
echo "<font
color='purple'>False<br></font>";}
if (!$a)
} else
{
echo "<font
color='purple'>False<br></font>";
}
34
Output:
Output:
35
PROGRAM:
<?php
// IF STATEMENT
$age = 24;
if ($age>18)
{
echo"<center><font color=red><font size=50>
Output:
37
PROGRAM:
<?php
// IF-ELSE STATEMENT
$age = 10;
if ($age>18)
{
echo"<center><font color=red><font size=50>
?>
38
Output:
39
<?php
// IF… ELSE IF… ELSE STATEMENT
$marks = 250;
{
echo "<center><font color='red'><font size='50'>Second</font></center>";
}
else if ($marks >= 135)
{
}
?>
40
Output:
41
Output:
Output:
43
PROGRAM:
<?php
$sum = 0;
$number = 2;
while ($number <= 10) {
$sum += $number;
$number += 2; }
Output:
Output:
45
PROGRAM:
<?php
$number = 1
do
{
echo"<center><font size=5><font color=purple>
$number <br>";
$number++;
}
while ($number <= 10);
?>
PROGRAM:
<?php
$cars=array("BMW","Jaguar","Bugatti","Ferrari");
foreach($cars as $value)
{
echo"<center><font color=purple><font size=50>
$value<br>";
}
?>
46
Output:
Output:
47
PROGRAM:
<?php
for ($num =1; $num<=2; $num++)
{
echo "<center><font color=blue><font size=5>
<b>Outer Loop: $num </b><br />";
}
?>
Output:
49
PROGRAM:
<?php
$number=5678;
if ($number%2==0)
{
echo "<center><font color=red><font size=50>
$number is an Even Number";
}
else
{
echo "<center><font color=blue><font size=50>
$number is Odd Number";
}
?>
50
Output:
51
{
$rem= $x % 10;
$total = $total + $rem * $rem * $rem;
$x= (int) ($x / 10);
}
if($num == $total)
{
echo"<center><font color=blue><font size=50>
Yes it is an Armstrong number";
}
else
{
echo"<center><font color=red><font size=50>
No it is not a Armstrong number";
}
?>
52
Output:
53
PROGRAM:
<?php
$number =0;
$a1=0;
$a2=1;
echo"<font color=><center><font size=50>
Fibonacci series for first 12 numbers: <br>";
echo "\n";
}
?>
54
Output:
55
}
echo "<br>";
}
for ($i=4; $i>=1; $i--)
{
{
echo "*";
}
echo "<br>";
}
?>
56
Output:
Output:
57
{
echo "*";
}
echo "<br>";
}
?>
<?php <?php
echo"<br><font color=orange>"."Welcome";
Hello World! <br>
?>
Welcome to php";
?>
58
Output:
Output:
59
<?php <?php
$num1 = 10;
PROGRAM:
<?php
function Message() //Defining a PHP Function
{
echo "<center><font color=blue><font size=50>
Hello,Welcome To Php";
}
Message(); //Calling a PHP Function
?>
60
Output:
Output:
61
PROGRAM:
<?php
PROGRAM:
<?php
function ABC($num1=101) //Defining a PHP Function
{
echo "<center><font color=orange><font size=50>
$num1.<br/>";
}
//Calling a PHP Function
ABC(303);
ABC();
?>
62
Output:
Output:
63
Hello<br/>";
}
$message = "ab";
//Dynamic Function Calls
$message();
?>
PROGRAM:
<?php // Call By Value
function abc($x) {
$x=$x+10;
return($x); }
$a=20;
echo "<center><font color=red><font size=50>";
echo abc($a)."<br>";
echo "<center><font color=purple><font size=50>";
echo($a)
?>
64
Output:
Output:
65
PROGRAM:
<?php // Call By Reference
function abc(&$x) {
$x=$x+10;
return($x); }
$a=20;
PROGRAM:
<?php
function display($number) {
if($number<=5) {
?>
66
Output:
67
PROGRAM:
<?php
//First method to create a numeric array.
$number = array(1,2,3,4,5);
foreach($number as $value)
{
$number[0] = "one";
$number[1] = "two";
$number[2] = "three";
$number[3] = "four";
$number[4] = "five";
foreach($number as $value)
{
echo "<center><font color=orange><font
size=5>
Value is $value<br/>";
}
?>
68
Output:
69
PROGRAM:
<?php
//First method to create an associative array.
$subject=array(
"English" => 65,
"Hindi" => 85,
$subject['Maths']="Medium";
echo "<center><font color=blue><font size=5>
Subject of English is : ".$subject['English']."<br/>";
echo "<center><font color=blue><font size=5>
Subject of Hindi is : ".$subject['Hindi']."<br/>";
Output:
71
PROGRAM:
<?php // Multidimensional array
$marks = array (
"Amit" => array (
"English" => 35,
"Hindi" => 30,
"Maths" => 39),
echo $marks['Amit']['English']."<br/>";
echo"<center><font color=purple><font size=5>
Marks for Aman in Hindi :";
echo $marks['Aman']['Hindi']."<br/>";
echo"<center><font color=blue><font size=5>
Output:
73
PROGRAM:
<?php
$cars=array("BMW","Audi","Ferrari");
$arrlength = count($cars)
for($x=0;$x<$arrlength;$x++)
{
echo"<br><center><font color=orange><font size=10>";
echo$cars[$x];
echo"<br>";
}
?>
74
Output:
75
PROGRAM:
<?php
$cars = array("BMW","Toyota","Maruti"); //Array Function
echo "<center><font color=red><font size=50>";
$arrlength=count($cars); // count Function
echo "<center><font color=red><font size=50>";
Output:
Output:
77
PROGRAM:
<?php
// Single Quoted Text
DOUBLE QUOTED:
PROGRAM:
<?php
// Double Quoted Text
$num1 = 10;
Output:
Output:
79
PROGRAM:
<?php
$drink = 'coffee';
echo "<center><font color=blue><font size=50>
Give me one $drink.<br>";
echo "<center><font color=orange><font size=50>
PROGRAM:
<?php
// Characters And String Indexes
$msg='ALL IS WELL';
for($index=0;$index<11;$index++)
{
$ch=$msg{$index};
echo"<br><center><font color=blue><font size=50>".$ch;
}
?>
80
Output:
Output:
81
?>
PROGRAM:
<?php // Concatenating Assignment Operator(.=)
$string1 = "Tishita";
$string2 = "Mathur";
Output:
83
PROGRAM:
<?php
$str2 = " Hello World!";
echo "<font color=red>";
echo "Without Trim: $str2 <font
echo strlen("Hello world")."<font
color=blue><br/>";
color=blue><br/>";
echo "With Trim: ".trim($str2)."<font
echo stripos("Hello world", "wo")."<font
color=red><br/>";
color=red><br/>";
echo strcmp("Kello world", "Hello
world")."<font color=blue><br/>"; // strtoupper() and strtolower()
Output:
Output:
85
PROGRAM:
<?php
$var = "variables";
$msg = <<<BCA
Hi, How are you all
<br>
Good
BCA;
echo"<center><font color=blue><font size=5>
$msg";
?>
PROGRAM:
<html>
<body>
<form name="f1">
</html>
86
Output:
Output:
87
FEEDBACK:<br><b>
<textarea name="fb" rows="5" cols="50">
Enter The Feedback About Your Teacher Here:
</textarea>
</form>
</body>
</html>
</body>
</html>
88
Output:
Output:
89
PROGRAM:
<html>
<body>
<form name="f1">
<label style="color: red;">
<input type="radio" name="r1" value="male">Male
</label><br>
<label style="color: blue;">
<input type="radio" name="r1" value="female">Female
</label><br>
</form>
</body>
</html>
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>
90
Output:
91
PROGRAM:
<html>
<body>
<form method="post">
<?php
if (isset($_POST['redirect_button']))
{
header("Location: https://www.google.com");
exit;
}
?>
</body>
</html>
92
Output:
Output:
93
{
$GLOBALS['z']=$GLOBALS['a']*$GLOBALS['y'];
}
multiplication();
echo"<font color=blue>".$z;
$_SERVER []
PROGRAM:
<?php
echo $_SERVER [ 'SERVER_NAME' ];
echo "<br>";
echo $_SERVER[ 'HTTP_HOST' ];
echo "<br>";
echo $_SERVER[ 'HTTP_USER_AGENT' ];
echo "<br>";
echo $_SERVER[ 'SCRIPT_NAME' ];
echo "<br>"
?>
94
Output:
95
$_REQUEST []
PROGRAM:
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_REQUEST['fname'];
if (empty($name))
{
echo "Name is empty";
}
Else
{
echo "Hello " . $name;
}
}
?>
</body>
</html>
96
Output:
97
PROGRAM:
<html>
<head>
<title> ADMISSION FORM</title>
</head>
<body>
<h2>Admission Form</h2>
Output:
99
PROGRAM:
<html>
<head>
// Check Connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
?>
</body>
</html>
100
Output:
101
PROGRAM:
<html>
<head>
<title>Close A Database connection With MySQL</title>
</head>
<body>
<?php
// Create Connection
$con = mysqli_connect("localhost", "root", "", "MYSQL");
// Check Connection
if (mysqli_connect_errno()) {
Output:
103
<?php
// Create Connection $con = mysqli_connect("localhost", "root", "");
// Check Connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " .mysqli_connect_error();
} else {
echo "<font color=red>Database Connection Successfully Build</font>"; }
// Create Database
$sql = "CREATE DATABASE BCA";
if (mysqli_query($con, $sql))
{
echo "<font color=blue><br>Database 'BCA' created successfully</font>";
} else {
echo "<br>Error creating database: " .mysqli_error($con);
}
// Close Connection
mysqli_close($con);
?>
</body>
</html>
104
Output:
105
} else {
echo "<font color='orange'>Database Connection Successfully Build</font>";
}
// SQL to Create Table
$sql = "CREATE TABLE STUDENT ( FirstName CHAR(30), LastName CHAR(30), Age INT )";
// Execute Query
if (mysqli_query($con, $sql)) {
echo "<font color='blue'><br>Table STUDENT Created Successfully</font>";
} else {
echo "<br>Error Creating Table: " . mysqli_error($con); // Close Connection
mysqli_close($con);
?>
</body>
</html>
106
Output:
107
else {
echo "<font color=purple> Database Connection Successfully Build";
}
//Inserting Records Into The Table
mysqli_query($con,"INSERT INTO STUDENT (FirstName,LastName,Age)
VALUES ('SEHAJ','MATHUR',15)");
mysqli_query($con,"INSERT INTO STUDENT (FirstName,LastName,Age)
VALUES ('SIMRAN','KAUR',20)");
mysqli_query($con,"INSERT INTO STUDENT (FirstName,LastName,Age)
VALUES ('PREM','KAUR'31)");
</html>
108
Output:
109
if (mysqli_num_rows($result) > 0) {
echo "<table>
<tr><th>First Name</th>
<th>Last Name</th>
<th>Age</th>
</tr>";
110
Output:
111
<?php
// Create Connection
$con=mysqli_connect("localhost","root","","BCA");
// Check Connection
if (mysqli_connect_errno())
{
echo "Failed To Connect to MySQL: ".mysqli_connect_error(); }
else {
echo "<font color=blue> Database Connection Successfully Build</font>";
}
?>
</body>
</html>
112
Output:
113
<head>
<title>Setting Cookies With PHP</title>
</head>
<body>
<?php echo "<font color=red><center> Set Cookies"?>
</body>
</html>
114
Output:
115
<head>
<title>Setting and Accessing Cookies with PHP</title>
</head>
<body>
<?php
Output:
Output:
117
<html>
<head>
<title> Deleting Cookies with PHP</title>
</head>
<body>
Output:
119
}
else
{
$_SESSION [ ' counter '] = 1;
}
<html>
</html>
120
Output:
121
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>
122
Output:
123
PROGRAM:
<?php
echo "<center><font color=red>";
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,
<a href="nextpage.php?<?php echo htmlspecialchars (SID); ?>">Click here<?a>.
</p>
124
Output:
125
PROGRAM:
<?php
session_start();
$_SESSION["name"] = "Tishita Mathur";
$_SESSION["city"] = "Mohali";
$enc_session = session_encode();
print "<b> <center><font color=blue> Encoded Session Data:<br/></b>";
print $enc_session."<br/><br/>";
// Changing Session Values
$_SESSION["name"] = "Sehaj kaur";
$_SESSION["city"] = "Chandigarh";
// Printing $_SESSION
?>