1
POST GRADUATE GOVERNMENT COLLEGE FOR GIRLS
SECTOR – 42, CHANDIGARH
(SESSION 2024-25)
PRACTICAL FILE OF PHP PROGRAMMING
(BCA-16-506)
Lab Based On BCA-16-504
SUBMITTED TO: SUBMITTED BY:
MRS. SARBJIT KAUR TISHITA MATHUR
ASSISTANT PROFESSOR ROLL NO -221546/22
DEPARTMENT OF COMPUTER BCA (5TH SEM)
APPLICATION
2
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
25. Write a program of switch statement. 40-41
26. Write a program to print a table using for 42-43
loop (Table Format is 2*1=2).
27. Write a program to print sum of even 42-43
numbers upto 10 using while loop.
28. Write a program to print first 10 numbers 44-45
using do-while loop.
29. Write a program of for each loop. 44-45
30. Write a program of nested for loop. 46-47
31. Write a program to print factorial number 46-47
of 5.
32. Write a program to check whether a given 48-49
number is even or odd.
33. Write a program in php to check whether a 50-51
number is Armstrong or not.
34. Write a program of Fibonacci sequence. 52-53
35. Write a program to print star triangle. 54-55
36. Write a program to print reverse star 56-57
triangle.
37. Write a program for include () function. 56-57
38. Write a program for require () function. 58-59
39. Write a program to create and invoke user 58-59
defined function.
40. Write a program of function with 60-61
arguments and returning values.
41. Write a program to setting default values 60-61
for function arguments.
42. Write a program of dynamic function call. 62-63
43. Write a program of call by value. 62-63
44. Write a program of call by reference. 64-65
45. Write a program of recursion. 64-65
46. Write a program of numeric array. 66-67
47. Write a program of associative array. 68-69
48. Write a program of multidimensional array. 70-71
49. Write a program to access the elements of 72-73
an array using simple method and loops.
50. Write a program of array functions. 74-75
4
51. Write a program of creating and accessing 76-77
string.
SINGLE QUOTED.
DOUBLE QUOTED.
52. Write a program of interpolation with curly 78-79
braces.
53. Write a program of characters and string 78-79
indexes.
54. Write a program of string operators. 80-81
CONCATENATION OPERATOR(.)
CONCATENATING ASSIGNMENT
OPERATOR (. = )
55. Write a program of string functions. 82-83
56. Write a program of Heredoc. 84-85
57. Write a program of text input. 84-85
58. Write a program of multi-line text input 86-87
control.
59. Write a program of checkbox. 86-87
60. Write a program of radio button. 88-89
61. Write a program of drop-down list. 88-89
62. Write a program to create a button. 90-91
63. Write a program of super global variables. 92-95
$GLOBALS []
$_SERVER []
$_REQUEST []
64. Write a program of HTML form and PHP 96-97
(Admission Form).
65. Write a program of connecting to database 98-99
(MYSQL).
66. Write a program to close a database 100-101
connection with (MYSQL).
67. Write a program of creating a database. 102-103
68. Write a program of creating a database 104-105
table.
69. Write a program of inserting records into 106-107
database table.
70. Write a program of selecting records from 108-109
database table.
5
71. Write a program of deleting records from 110-111
database table.
72. Write a program of setting / creating 112-113
cookies with php.
73. Write a program of accessing cookies with 114-115
php.
74. Write a program of deleting a cookie. 116-117
75. Write a program of creating session cookie. 116-117
76. Write a program of registering session 118-119
variable.
77. Write a program of working with session 120-121
variable.
78. Write a program of SID constant. 122-123
79. Write a program of encoding and decoding 124-125
session variable.
6
Output:
Output:
7
1. WRITE A PROGRAM TO PRINT A MESSAGE.
PROGRAM:
<?php
echo "<font size=50><center><font color=indigo>
Hello World! <br>
Welcome to php";
?>
2. WRITE A PROGRAM TO ADD TWO NUMBERS.
PROGRAM:
<?php
$num1 = 10;
$num2 = 20;
$sum = $num1 + $num2;
echo "<font color=red><center><font size=50>
Addition of two numbers is: <br>";
echo "<font size=20>".$num1." + ".$num2." = ".$sum;
?>
8
Output:
Output:
9
3. WRITE A PROGRAM TO FIND AVERAGE OF FIVE
NUMBERS.
PROGRAM:
<?php
$a=50;
$b=60;
$c=70;
$d=50;
$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;
echo "<font color=blue><center><font size=50>
Average of five numbers is: ".$e."\n";
?>
4. WRITE A PROGRAM TO FIND SIMPLE INTEREST.
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
5. WRITE A PROGRAM TO FIND AN AREA OF
TRIANGLE.
PROGRAM:
<?php
// Area Of Triangle
$base = 10;
$height = 15;
$area= ($base * $height)/2;
echo"<center><font size=10><font color=blue> Area of Triangle
with base</font color>" .$base. "<font color=blue> and
height</font color>".$height. "=<font color=blue>" .$area;
?>
6. WRITE A PROGRAM TO FIND AN AREA OF
RECTANGLE.
PROGRAM:
<?php
//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
7. WRITE A PROGRAM TO FIND AN AREA OF
CIRCLE.
PROGRAM:
<?php
//Area of circle
$r=4;
$pi=3.14;
$a=$pi*$r*$r;
echo "<center><font color=red><font size=50>
Area of circle is: ", $a;
?>
8. WRITE A PROGRAM TO DISPLAY A STATIC
VARIABLE.
PROGRAM:
<?php
function displayStaticVariable()
{
static $count = 0;
$count++;
echo "<center><font size='10' color='red'><br>The count is: $count</font></center>";
}
displayStaticVariable();
displayStaticVariable();
displayStaticVariable();
displayStaticVariable();
displayStaticVariable();
?>
14
Output:
15
9. WRITE A PROGRAM FOR SWAPPING WITHOUT
USING THIRD VARIABLE.
PROGRAM:
<?php
$num1 = 10;
$num2 = 20;
echo "<font color=blue><center><font size=5> <br>
Swapping two numbers";
echo "<font color=orange><center><font size=5> <br>
Before Swap";
echo "<font color=blue><center><font size=5> <br>
Value of first number is = ". $num1;
echo "<font color=orange><center><font size=5> <br>
Value of second number is = ". $num2;
$num1 = $num1 - $num2;
$num2 = $num1 + $num2;
$num1 = $num2 - $num1;
echo "<font color=blue><center><font size=5> <br>
After Swap";
echo "<font color=orange><center><font size=5> <br>
Value of first number is = ". $num1;
echo "<font color=blue><center><font size=5> <br>
Value of second number is = ". $num2;
?>
16
Output:
17
10. WRITE A PROGRAM FOR SWAPPING OF
NUMBERS.
PROGRAM:
<?php
$x=50; // SWAPPING OF TWO NUMBERS
$y=60;
echo"<font size=30><font color=orange>Before Swapping:</font
color></font size> <br>";
echo"<font size=20> x =".$x." y=".$y;
$third=$x;
$x=$y;
//SWAPPING LOGIC
$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
11. WRITE A PROGRAM TO DISPLAY LOCAL
VARIABLE.
PROGRAM:
<?php
function display()
{
$a = 10; //Local Variable
echo "<font color=blue><center><font size=50> <br>
Accessing value inside function = $a";
}
display();
?>
12. WRITE A PROGRAM TO ACCESS THE GLOBAL
VARIABLE INSIDE THE FUNCTION.
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";
?>
20
Output:
21
13. WRITE A PROGRAM TO DISPLAY GLOBAL
VARIABLE.
PROGRAM:
<?php
$x=5; //GLOBAL VARIABLE
function Test ()
$y=6; //LOCAL VARIABLE
echo"<center><font size=50><font color=green>
<p>Test variable inside the function:</p>";
echo "<br>";
echo "<center><font size=50><font color=purple>
Variable y is:".$y;
}
Test ();
echo"<center><font size=50><font color=green>
<p>Test variable outside the function:</p>";
echo "<center><font size=50><font color=purple>
Variable x is :".$x;
echo"<br>";
?>
22
Output:
Output:
23
14. WRITE A PROGRAM OF CONSTANT
CASE-SENSITIVE CONSTANT.
PROGRAM:
<?php
//define a Case - Sensitive Constant
define("GREETING","<center><font color= blue><font size=10>Welcome you all");
echo GREETING;
echo"<br>";
// will not output the value of the constant
echo greeting;
?>
15. WRITE A PROGRAM OF CONSTANT
CASE-INSENSITIVE 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
16. WRITE A PROGRAM OF ARITHMETIC
OPERATOR.
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>";
echo"<b><center><font size = 10><font color = blue>";
echo"Multiplication :" .($a * $b) ."<br>";
echo"<b><center><font size = 10><font color = orange>";
echo"Division :" .($a / $b) ."<br>";
echo"<b><center><font size = 10><font color = blue>";
echo"Remainder :" .($a % $b) ."<br>";
?>
26
Output:
27
17. WRITE A PROGRAM OF INCREMENT AND
DECREMENT OPERATOR.
PROGRAM:
<?php
$x = 50;
echo "<font color=red><center><b>Initial value of x: $x <br>";
$y = $x++;
echo "<b><center><font color=blue>Value of y: $y <br>";
// Post-increment
echo "<font color=red><center>Value of x after post-increment: $x <br>";
$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--;
echo "<font color=blue><center>Value of v: $v <br>";
echo "<font color=red><center>Value of x after post-decrement: $x <br>";
// Pre-decrement
$q = --$x;
echo "<font color=blue><center>Value of e: $q <br>";
echo "<font color=red><center>Value of x after pre-decrement: $x <br>";
?>
28
Output:
29
18. WRITE A PROGRAM OF COMPARISON
OPERATOR.
PROGRAM:
<?php echo "<font
$a = 10; // Two Variables color='blue'>True<br></font>";
$b = 5; // Comparison operators } else {
echo "<b><center><font size='6'>Equal echo "<font
(==): "; color='black'>False<br></font>";
if ($a == $b) { } echo "Less than (<): ";
echo "<font if ($a < $b) {
color='blue'>True<br></font>"; } echo "<font
else { color='blue'>True<br></font>";
echo "<font } else {
color='black'>False<br></font>"; echo "<font
} color='black'>False<br></font>"; }
echo "Not Equal (!=): "; echo "Greater than or equal to (>=): ";
if ($a != $b) if ($a >= $b) {
{ 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
19. WRITE A PROGRAM OF ASSIGNMENT
OPERATOR.
PROGRAM:
<?php
//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
20. WRITE A PROGRAM OF LOGICAL OPERATOR.
PROGRAM:
<?php // Combining Logical Operators
$a = true; // First condition echo "Combining AND and OR: ";
$b = false; // Second condition if (($a && !$b) || $b)
// AND (&&) Operator {
echo "<b><center><font size='6'>Logical AND echo "<font
(&&): "; color='orange'>True<br></font>";
if ($a && $b) }
{ 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
echo "Logical OR (||): ";
if ($a || $b)
echo "<font color='orange'>True<br></font>";
} else {
echo "<font
color='purple'>False<br></font>";}
// NOT (!) Operator
echo "Logical NOT (!): ";
if (!$a)
echo "<font color='orange'>True<br></font>";
} else
{
echo "<font
color='purple'>False<br></font>";
}
34
Output:
Output:
35
21. WRITE A PROGRAM OF CONDITIONAL
OPERATOR.
PROGRAM:
<?php
//conditional operator
$a=10;
$b=20;
echo($a>$b)?"<center><font color=red><font size=50>
$a is greater than $b":"<center><font color=blue><font size=50>
$b is greater than $a";
?>
22. WRITE A PROGRAM OF IF STATEMENT.
PROGRAM:
<?php
// IF STATEMENT
$age = 24;
if ($age>18)
{
echo"<center><font color=red><font size=50>
Congratulations: You are in the list of voter";
}
?>
36
Output:
37
23. WRITE A PROGRAM OF IF-ELSE STATEMENT.
PROGRAM:
<?php
// IF-ELSE STATEMENT
$age = 10;
if ($age>18)
{
echo"<center><font color=red><font size=50>
Congratulations: You are in the list of voter";
}
else
echo "<center><font color=blue><font size=50>
You are below age, so you are not in the list of voter"
?>
38
Output:
39
24. WRITE A PROGRAM OF IF-ELSE IF-ELSE
STATEMENT.
PROGRAM:
<?php
// IF… ELSE IF… ELSE STATEMENT
$marks = 250;
if ($marks >= 300)
{
echo "<center><font color='orange' <font size='50'>First</font></center>";
}
else if ($marks >= 250)
{
echo "<center><font color='red'><font size='50'>Second</font></center>";
}
else if ($marks >= 135)
{
echo "<center><font color='green'> <font size='50'>Third</font></center>";
}
else
{
echo "<center><font color='blue'> <font size='50'>Fail</font></center>";
}
?>
40
Output:
41
25. WRITE A PROGRAM OF SWITCH STATEMENT.
PROGRAM:
<?php case "Fri":
$d=date("D"); echo "<center><font size=50><font
color=blue>
switch($d)
Today is Friday";
{
break;
case "Mon":
case "Sat":
echo "<center><font size=50><font
color=maroon> echo "<center><font size=50><font
color=brown>
Today is Monday";
Today is Saturday";
break;
break;
case "Tue":
case "Sun":
echo "<center><font size=50><font
color=orange> echo "<center><font size=5><font
color=teal>
Today is Tuesday";
Today is Sunday";
break;
break;
case "Wed":
default:
echo "<center><font size=50><font
color=green> echo "<center><font size=5><font
color=burgundy>
Today is Wednesday";
Wonder which day is this";
break;
}
case "Thu":
?>
echo "<center><font size=50><font
color=purple>
Today is Thursday";
break;
42
Output:
Output:
43
26. WRITE A PROGRAM TO PRINT A TABLE USING
FOR LOOP (TABLE FORMAT IS 2*1=2).
PROGRAM:
<?php
// Print a table using for loop (table format is 2*1=2)
$number = 2;
for ($i = 1; $i <= 10; $i++)
{
$product = $number * $i;
echo "<center><font color=blue><font size=5>
$number * $i = $product<br>";
}
?>
27. WRITE A PROGRAM TO PRINT SUM OF EVEN
NUMBERS UPTO 10 USING WHILE LOOP.
PROGRAM:
<?php
$sum = 0;
$number = 2;
while ($number <= 10) {
$sum += $number;
$number += 2; }
echo "<center><font size=50><font color=orange>
The sum of even numbers up to 10 is: $sum";
?>
44
Output:
Output:
45
28. WRITE A PROGRAM TO PRINT FIRST 10
NUMBERS USING DO-WHILE LOOP.
PROGRAM:
<?php
$number = 1
do
{
echo"<center><font size=5><font color=purple>
$number <br>";
$number++;
}
while ($number <= 10);
?>
29. WRITE A PROGRAM OF FOR EACH LOOP.
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
30. WRITE A PROGRAM OF NESTED FOR LOOP.
PROGRAM:
<?php
for ($num =1; $num<=2; $num++)
{
echo "<center><font color=blue><font size=5>
<b>Outer Loop: $num </b><br />";
for($val = 1; $val <=3; $val++)
{
echo "<center><font color=red><font size=5>
Inner Loop: $val <br />";
}
}
?>
31. WRITE A PROGRAM TO PRINT FACTORIAL
NUMBER OF 5.
PROGRAM:
<?php
$num = 5;
$factorial = 1;
for ($x=$num; $x>=1; $x--) {
$factorial = $factorial * $x; }
echo "<center><font color=purple><font size=50>
Factorial of $num is $factorial";
?>
48
Output:
49
32. WRITE A PROGRAM TO CHECK WHETHER A
GIVEN NUMBER IS EVEN OR ODD.
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
33. WRITE A PROGRAM IN PHP TO CHECK
WHETHER A NUMBER IS ARMSTRONG OR NOT.
PROGRAM:
<?php
$num = 407;
$total = 0;
$x = $num;
while($x != 0)
{
$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
34. WRITE A PROGRAM OF FIBONACCI
SEQUENCE.
PROGRAM:
<?php
$number =0;
$a1=0;
$a2=1;
echo"<font color=><center><font size=50>
Fibonacci series for first 12 numbers: <br>";
echo "\n";
echo $a1.' '.$a2.' ';
while ($number<10)
{
$a3=$a2 + $a1;
echo $a3. ' ';
$a1=$a2;
$a2=$a3;
$number=$number+1;
}
?>
54
Output:
55
35. WRITE A PROGRAM TO PRINT STAR
TRIANGLE.
PROGRAM:
<?php
for ($i=0; $i<=5; $i++)
{
for ($k=5; $k>=$i; $k--)
{
echo " ";
}
for ($j=1; $j<=$i; $j++)
{
echo "*";
}
echo "<br>";
}
for ($i=4; $i>=1; $i--)
{
for ($k=5; $k>=$i; $k--)
{
echo " ";
}
for ($j=1; $j<=$i; $j++)
{
echo "*";
}
echo "<br>";
}
?>
56
Output:
Output:
57
36. WRITE A PROGRAM TO PRINT REVERSE STAR
TRIANGLE.
PROGRAM:
<?php
$rows = 5;
for ($i = $rows; $i >= 1; $i--)
{
for ($j = 1; $j <= $i; $j++)
{
echo "*";
}
echo "<br>";
}
?>
37. WRITE A PROGRAM FOR INCLUDE ( )
FUNCTION
PROGRAM:
INCLUDE FUNCTION () MSG.PHP
<?php <?php
include("msg.php"); echo "<font size=50><center><font
color=indigo>
echo"<br><font color=orange>"."Welcome";
Hello World! <br>
?>
Welcome to php";
?>
58
Output:
Output:
59
38. WRITE A PROGRAM FOR REQUIRE ( )
FUNCTION
PROGRAM:
REQUIRE FUNCTION () ADD.PHP
<?php <?php
$num1 = 10;
require("Add.php"); $num2 = 20;
$sum = $num1 + $num2;
echo"<br><font echo "<font
color=blue>"."BCA"; color=red><center><font size=50>
?> Addition of two numbers is: <br>";
echo "<font size=20>".$num1." +
".$num2." = ".$sum;
?>
39. WRITE A PROGRAM TO CREATE AND
INVOKE USER DEFINED FUNCTION.
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
40. WRITE A PROGRAM OF FUNCTION WITH
ARGUMENTS AND RETURING VALUES.
PROGRAM:
<?php
function addFunction($num1, $num2) // Defining a PHP Function {
$sum = $num1 + $num2; //Here $num1 and $num2 are formal parameters.
return $sum; } //Calling a PHP Function
$result = addFunction(11,22); //Here, 11 and 22 are actual parameters.
echo "<center><font color=green><font size=50>
The Result Is : " .$result;
?>
41. WRITE A PROGRAM TO SETTING DEFAULT
VALUES FOR FUNCTION ARGUMENTS.
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
42. WRITE A PROGRAM OF DYNAMIC FUNCTION
CALL.
PROGRAM:
<?php
function ab() //Defining a PHP Function
{
echo "<center><font color=blue><font size=50>
Hello<br/>";
}
$message = "ab";
//Dynamic Function Calls
$message();
?>
43. WRITE A PROGRAM OF CALL BY VALUE.
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
44. WRITE A PROGRAM OF CALL BY REFERENCE.
PROGRAM:
<?php // Call By Reference
function abc(&$x) {
$x=$x+10;
return($x); }
$a=20;
echo "<center><font color=blue><font size=50>";
echo abc($a)."<br>";
echo "<center><font color=orange><font size=50>";
echo($a);
?>
45. WRITE A PROGRAM OF RECURSION.
PROGRAM:
<?php
function display($number) {
if($number<=5) {
echo "<center><font color=green><font size=50>
$number <br/>";
display($number+1); }
}
display(1);
?>
66
Output:
67
46. WRITE A PROGRAM OF NUMERIC ARRAY.
PROGRAM:
<?php
//First method to create a numeric array.
$number = array(1,2,3,4,5);
foreach($number as $value)
{
echo "<center><font color=purple><font
size=5>
Value is $value<br/>";
}
//Second method to create a numeric array.
$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
47. WRITE A PROGRAM OF ASSOCIATIVE ARRAY.
PROGRAM:
<?php
//First method to create an associative array.
$subject=array(
"English" => 65,
"Hindi" => 85,
"Maths" => 75);
echo "<center><font color=orange><font size=5>
Subject of English is : ".$subject['English']."<br/>";
echo "<center><font color=orange><font size=5>
Subject of Hindi is : ".$subject['Hindi']."<br/>";
echo "<center><font color=orange><font size=5>
Subject of Maths is : ".$subject['Maths']."<br/>";
//Second method to create an associative array.
$subject['English']="Low";
$subject['Hindi']="High";
$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/>";
echo "<center><font color=blue><font size=5>
Subject of Maths is : ".$subject['Maths']."<br/>";
?>
70
Output:
71
48. WRITE A PROGRAM OF MULTIDIMENSIONAL
ARRAY.
PROGRAM:
<?php // Multidimensional array
$marks = array (
"Amit" => array (
"English" => 35,
"Hindi" => 30,
"Maths" => 39),
"Aman" => array (
"English" => 30,
"Hindi" => 32,
"Maths" => 29),
"Ajay" => array (
"English" => 31,
"Hindi" => 22,
"Maths" => 39) );
echo"<center><font color=red><font size=5>
Marks for Amit in English :";
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>
Marks for Ajay in Maths :";
echo $marks['Ajay']['Maths']."<br/>";
?>
72
Output:
73
49. WRITE A PROGRAM TO ACCESS THE
ELEMENTS OF AN ARRAY USING SIMPLE
METHOD AND LOOPS.
PROGRAM:
<?php
$cars=array("BMW","Audi","Ferrari");
//count()function returns the number of elements in an array
$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
50. WRITE A PROGRAM OF ARRAY FUNCTIONS.
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>";
echo current($cars)."<br>"; //current Function
echo "<center><font color=red><font size=50>";
echo next($cars)."<br>"; //next Function
echo "<center><font color=red><font size=50>";
echo prev($cars)."<br>"; //prev Function
for($x = 0; $x < $arrlength; $x++)
{
echo $cars[$x];
echo "<br>";
}
echo "<center><font color=red><font size=50>";
echo reset($cars)."<br>"; //reset Function
echo "<center><font color=red><font size=50>";
echo end($cars)."<br>"; //end Function
?>
76
Output:
Output:
77
51. WRITE A PROGRAM OF CREATING AND
ACCESSING STRING.
SINGLE QUOTED:
PROGRAM:
<?php
// Single Quoted Text
$str='Text Within Single Quote' ;
echo"<br><center><font color=orange><font size=10>";
echo $str;
?>
DOUBLE QUOTED:
PROGRAM:
<?php
// Double Quoted Text
$num1 = 10;
$str1 = "Multiple lines
text within
double quoted string with a variable-value $num1";
$str2 = "Using escape sequence \n in double quoted string";
$str3 = "Using double \"quote\" inside double quoted string"; // Proper escaping for
quotes
echo "<center><font color=blue><font size=50>";
echo "First String is: $str1"; // string concatenation
echo "<br/>$str2 <br/>$str3"; // Added line breaks
?>
78
Output:
Output:
79
52. WRITE A PROGRAM OF INTERPOLATION
WITH CURLY BRACES.
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>
Give me two $drinks.<br>"; // Won't work, treated $drinks as new variable
echo "<center><font color=blue><font size=50>
Give me two {$drink}s.<br>";
?>
53. WRITE A PROGRAM OF CHARACTERS AND
STRING INDEXES.
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
54. WRITE A PROGRAM OF STRING OPERATORS.
CONCATENATION OPERATOR (.)
PROGRAM:
<?php // Concatenation Operator(.)
$string1 = "TISHITA";
$string2 = "MATHUR";
echo "<font color=navy blue> ";
echo "------------------------"; // Line break with asterisks
echo"<br><font color=navy blue>";
echo $string1 . " " . $string2; // Concatenated string
echo "<br><font color=navy blue> ";
echo "------------------------"; // Line break with asterisks
?>
CONCATENATING ASSIGNMENT OPERATOR (.=)
PROGRAM:
<?php // Concatenating Assignment Operator(.=)
$string1 = "Tishita";
$string2 = "Mathur";
echo "<font color=red> ";
echo "------------------------"; // Line break with asterisks
echo"<br><font color=red>";
$string1 .= $string2;
echo $string1;
echo "<br><font color=red> ";
echo "------------------------"; // Line break with asterisks
?>
82
Output:
83
55. WRITE A PROGRAM OF STRING FUNCTIONS.
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()
echo strncmp("Hello world", "Hellp earth", echo strtoupper("hello
6)."<font color=red><br/>"; world")."<font color=blue><br/>"; //
Converts to uppercase
echo strrev("Hello World")."<font
color=blue><br/>"; echo strtolower("HELLO
WORLD")."<font color=red><br/>";
echo strrpos("Hello world Hello world",
// Converts to lowercase
"wo")."<font color=blue><br/>";
// ucfirst() and ucwords()
// chr() function
echo ucfirst("hello world")."<font
echo chr(65)."<font color=red><br/>"; //
color=blue><br/>"; // Capitalizes first
Outputs character 'A'
character
// ltrim() and rtrim() functions
echo ucwords("hello world")."<font
$str = " Hello World!"; color=red><br/>"; // Capitalizes first
character of each word
echo "Without Ltrim: $str <font
color=blue><br/>"; // wordwrap() function
echo "With Ltrim: ".ltrim($str)."<font $str3 = "The wordwrap() function
color=red><br/>"; wraps a string into new lines when it
reaches a specific length";
$str1 = " Hello World";
echo wordwrap($str3, 15,
echo "Without Rtrim: $str1 <font
"<br>")."<font color=blue><br/>"; //
color=blue><br/>";
Wraps text at 15 characters
echo "With Rtrim: ".rtrim($str1)."<font
?>
color=red><br/>";
84
Output:
Output:
85
56. WRITE A PROGRAM OF HEREDOC.
PROGRAM:
<?php
$var = "variables";
$msg = <<<BCA
Hi, How are you all
<br>
Good
BCA;
echo"<center><font color=blue><font size=5>
$msg";
?>
57. WRITE A PROGRAM OF TEXT INPUT.
PROGRAM:
<html>
<body>
<form name="f1">
<font color="blue">First name: <input type="text" name="fname">
<br><br>
<font color="red">Last name: <input type="text" name="1name">
</form>
</body>
</html>
86
Output:
Output:
87
58. WRITE A PROGRAM OF MULTI LINE TEXT
INPUT CONTROL.
PROGRAM:
<html>
<body>
<form name="f1">
FEEDBACK:<br><b>
<textarea name="fb" rows="5" cols="50">
Enter The Feedback About Your Teacher Here:
</textarea>
</form>
</body>
</html>
59. WRITE A PROGRAM OF CHECKBOX.
PROGRAM:
<html>
<body>
<form name="f1">
<label style="color: red;"> <input type="checkbox" name="DM" value="on">DM
</label><br> <label style="color: blue;">
<font colour="blue"><input type="checkbox" name="PHP" value="on">PHP </label><br>
<label style="color: purple;">
<font colour="purple"><input type="checkbox" name="JAVA" value="on">JAVA
</label><br>
</form>
</body>
</html>
88
Output:
Output:
89
60. WRITE A PROGRAM OF RADIO BUTTON.
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>
61. WRITE A PROGRAM OF DROP COLUMN LIST
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
62. WRITE A PROGRAM TO CREATE A BUTTON.
PROGRAM:
<html>
<body>
<form method="post">
<button type="submit" name="redirect_button">Go to Google</button>
</form>
<?php
if (isset($_POST['redirect_button']))
{
header("Location: https://www.google.com");
exit;
}
?>
</body>
</html>
92
Output:
Output:
93
63. WRITE A PROGRAM OF SUPER GLOBAL
VARIABLES.
$GLOBALS []
PROGRAM:
<?php
$a = 30;
$y = 50;
function multiplication()
{
$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
64. WRITE A PROGRAM OF HTML FORM AND
PHP (ADMISSION FORM).
PROGRAM:
<html>
<head>
<title> ADMISSION FORM</title>
</head>
<body>
<h2>Admission Form</h2>
<form action="admission.php" method="post">
Full Name: <input type="text" name="name"><br><br>
Father Name: <input type="text" name="name"><br><br>
Mother Name: <input type="text" name="name"><br><br>
E-mail: <input type="text" name="email"><br><br>
Contact Number:<input type="text" id="contact" name="contact"><br><br>
Address: <input type="text> name="text" name="address"><br><br>
Date of Birth:<input type="date" id="dob" name="dob"><br><br>
Gender:<input type="radio" id="female" name="gender" value="female">Female
<input type="radio" id="male" name="gender" value="male">Male<br><br>
<label for="course">Select Course:</label>
<select id="course" name="course" required>
<option value="BCA">BCA</option>
<option value="BBA">BBA</option>
<option value="BA">BA</option>
<option value="B.Sc">B.Sc</option> </select><br><br>
<input type="submit" value="Submit">
</form> </body> </html>
98
Output:
99
65. WRITE A PROGRAM OF CONNECTING TO
DATABASE (MYSQL)
PROGRAM:
<html>
<head>
<title>Database Connection With MySQL</title>
</head>
<body>
<?php
// Create Connection
$con = mysqli_connect("localhost", "root", "", "MYSQL");
// 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>
100
Output:
101
66. WRITE A PROGRAM TO CLOSE A DATABASE
CONNECTION WITH (MYSQL)
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()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else {
echo "<font color=blue>Database Connection Successfully Built</font>";
}
// Close the connection
echo "<font color=red><br>Database Connection Successfully closed</font>";
?>
</body>
</html
102
Output:
103
67. WRITE A PROGRAM OF CREATING A
DATABASE
PROGRAM:
<html>
<head>
<title>Create A Database</title>
</head>
<body>
<?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
68. WRITE A PROGRAM OF CREATING A
DATABASE TABLE.
PROGRAM:
<html>
<head>
<title>Create A Table</title>
</head>
<body>
<?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='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
69. WRITE A PROGRAM OF INSERTING RECORDS
INTO DATABASE TABLE.
PROGRAM:
<html>
<head>
<title>Inserting Records Into Database Table</title>
</head>
<body>
<?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=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)");
echo "<font color=red><br>Records Have Been Successfully Inserted";
// Close Connection
mysqli_close($con);
?>
</body>
</html>
108
Output:
109
70. WRITE A PROGRAM OF SELECTING RECORDS
FROM DATABASE TABLE.
PROGRAM:
<html>
// Fetch and display records in a table
<head>
while ($row =
<title>Selecting Records From Database mysqli_fetch_array($result)) {
Table</title>
echo "<tr>
</head>
<td>{$row['FirstName']}</td>
<body>
<td>{$row['LastName']}</td>
<?php
<td>{$row['Age']}</td></tr>";
// Create Connection
}
$con = mysqli_connect("localhost", "root", "",
"BCA");
echo "</table>";
// Check Connection
} else {
if (mysqli_connect_errno()) {
echo "<p style='text-align:center;
echo "Failed to connect to MySQL: " .
color:red;'>No records found in the
mysqli_connect_error();
STUDENT table.</p>";
exit(); // Stop execution if connection fails
}
} else {
// Close Connection
echo "<p style='text-align:center;
mysqli_close($con);
color:blue;'>Database Connection Successfully
Build</p>"; ?>
} </body>
// Select Records from the STUDENT Table </html>
$result = mysqli_query($con, "SELECT * FROM
STUDENT");
// Check if records exist
if (mysqli_num_rows($result) > 0) {
echo "<table>
<tr><th>First Name</th>
<th>Last Name</th>
<th>Age</th>
</tr>";
110
Output:
111
71. WRITE A PROGRAM OF DELETING RECORDS
FROM DATABASE TABLE.
PROGRAM:
<html>
<head>
<title>Deleting Records From Database Table</title>
</head>
<body>
<?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>";
}
// Delete Records From The Table
mysqli_query($con,"DELETE FROM STUDENT");
echo "<font color=red><br>Records Have Been Successfully Deleted";
// Close Connection
mysqli_close($con);
?>
</body>
</html>
112
Output:
113
72. WRITE A PROGRAM OF SETTING / CREATING
COOKIES WITH PHP.
PROGRAM:
<?php
setcookie("name","Khushi",time()+3600,"/","",0);
setcookie("age","20", time()+3600,"/","",0);
?>
<html>
<head>
<title>Setting Cookies With PHP</title>
</head>
<body>
<?php echo "<font color=red><center> Set Cookies"?>
</body>
</html>
114
Output:
115
73. WRITE A PROGRAM OF ACCESSING COOKIES
WITH PHP.
PROGRAM:
<?php
setcookie("name","Tishita Mathur",time()+3600,"/","",0);
setcookie("age","20", time()+3600,"/","",0);
?>
<html>
<head>
<title>Setting and Accessing Cookies with PHP</title>
</head>
<body>
<?php
echo "<font color=red><center>";
echo $_COOKIE["name"]. "<br />";
echo "<font color=blue><center>";
echo $_COOKIE["age"]. "<br />";
?>
116
Output:
Output:
117
74. WRITE A PROGRAM OF DELETING A COOKIE.
PROGRAM:
<?php
//Set the expiration date to one hour ago
setcookie("name","", time()- 3600, "/","",0);
setcookie("age","", time()- 3600, "/","",0); ?>
<html>
<head>
<title> Deleting Cookies with PHP</title>
</head>
<body>
<?php echo "<font color=orange><center> Deleted Cookies"?>
</body>
</html>
75. WRITE A PROGRAM OF CREATING SESSION
COOKIE.
PROGRAM:
<?php
setcookie("name", "Tishita"); ?>
<html>
<head>
<title>Creating Session Cookie with PHP</title>
</head> <body>
<?php echo "<center><font color=blue>Set Session Cookies"?>
</body>
</html>
118
Output:
119
76. WRITE A PROGRAM OF REGISTERING
SESSION VARIABLE.
PROGRAM:
<?php
session_start ( );
if ( isset ($_SESSION [ ' counter ' ] ) )
{
$_SESSION [ ' counter '] += 1;
}
else
{
$_SESSION [ ' counter '] = 1;
}
$msg = "You have visited ".$_SESSION [ ' counter '];
$msg .= " times this page in current session.";
?>
<html>
<title>Setting up a PHP session</title>
</head>
<body>
<?php echo "<center><font color=blue><b>".($msg); ?>
</body>
</html>
120
Output:
121
77. WRITE A PROGRAM OF WORKING WITH
SESSION VARIABLES.
PROGRAM:
First Page (“session1.php”) Second Page (“session2.php”)
<?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
78. WRITE A PROGRAM OF SID CONSTANT.
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
79. WRITE A PROGRAM OF ENCODING AND
DECODING SESSION VARIABLE.
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
print "<b> <center><font color=red> SESSION Array:<br/></b>";
print "<pre>";
print_r($_SESSION);
print "</pre>";
session_decode ($enc_session);
//Printing Reloaded $_SESSION
print "<b> <center><font color=purple> Reloaded SESSION Array:<br/></b>";
print "<pre>";
print_r($_SESSION);
print "</pre>";
?>