0% found this document useful (0 votes)
23 views

PHP Practical File 1

Uploaded by

Khushi Thakur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

PHP Practical File 1

Uploaded by

Khushi Thakur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 158

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.
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

24. Write a program of if-elseif-else statement.


25. Write a program of switch statement.
26. Write a program to print a table using for
loop (Table Format is 2*1=2).
27. Write a program to print sum of even
numbers upto 10 using while loop.
28. Write a program to print first 10 numbers
using do-while loop.
29. Write a program of for each loop.
30. Write a program of nested for loop.
31. Write a program to print factorial number
of 5.
32. Write a program to check whether a given
number is even or odd.
33. Write a program in php to check whether a
number is Armstrong or not.
34. Write a program of Fibonacci sequence.
35. Write a program to print star triangle.
36. Write a program to print reverse star
triangle.
37. Write a program for include () function.
38. Write a program for require () function.
39. Write a program to create and invoke user
defined function.
40. Write a program of function with
arguments and returning values.
41. Write a program to setting default values
for function arguments.
42. Write a program of dynamic function call.
43. Write a program of call by value.
44. Write a program of call by reference.
45. Write a program of recursion.
46. Write a program of numeric array.
47. Write a program of associative array.
48. Write a program of multidimensional array.
49. Write a program to access the elements of
an array using simple method and loops.
4

50. Write a program of array functions.


51. Write a program of creating and accessing
string.
 SINGLE QUOTED.
 DOUBLE QUOTED.
52. Write a program of interpolation with curly
braces.
53. Write a program of characters and string
indexes.
54. Write a program of string operators.
 CONCATENATION OPERATOR(.)
 CONCATENATING ASSIGNMENT
OPERATOR (. = )
55. Write a program of string functions.
56. Write a program of Heredoc.
57. Write a program of text input.
58. Write a program of multi-line text input
control.
59. Write a program of checkbox.
60. Write a program of radio button.
61. Write a program of drop-down list.
62. Write a program to create a button.
63. Write a program of super global variables.
 $GLOBALS []
 $_SERVER []
 $_REQUEST []
64. Write a program of HTML form and PHP
(Admission Form).
65. Write a program to open a connection to
the mysql server and close a connection.
66. Write a program to creating a database
and database table.
67. Write a program to insert data into a
database table.
68. Write a program to select data from a
database.
69. Write a program to delete data from a
database table.
5

70. Write a program of setting / creating


cookies with php.
71. Write a program of accessing cookies with
php.
72. Write a program of deleting a cookie.
73. Write a program of creating session cookie.
74. Write a program of registering session
variable.
75. Write a program of working with session
variables.
6

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";

?>
8

Output:
9

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;

?>
10

Output:
11

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";

?>
12

Output:
13

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";

?>
14

Output:
15

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;

?>
16

Output:
17

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>";

?>
18

Output:
19

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;

?>
20

Output:
21

8. 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;

?>
22

Output:
23

9. WRITE A PROGRAM FOR SWAPPING OF


NUMBER.
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;

?>
24

Output:
25

10. 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();

?>
26

Output:
27

11. 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>";

?>
28

Output:
29

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";
?>
30

Output:
31

13. 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();

?>
32

Output:
33

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;

?>
34

Output:
35

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;

?>
36

Output:
37

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>";

?>
38

Output:
39

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>";

?>
40

Output:
41

18. WRITE A PROGRAM OF COMPARISON


OPERATOR.
PROGRAM:

<?php // Additional comparison examples

$a = 10; // Two Variables echo "Greater than (>): ";

$b = 5; if ($a > $b)

// Comparison operators echo "<font color='blue'>True<br></font>";

echo "<b><center><font size='6'>Equal (==): } else {


";
echo "<font color='black'>False<br></font>";
if ($a == $b)
} echo "Less than (<): ";
{
if ($a < $b) {
echo "<font color='blue'>True<br></font>";
echo "<font color='blue'>True<br></font>";
}
} else {
else
echo "<font color='black'>False<br></font>";
{
}
echo "<font
echo "Greater than or equal to (>=): ";
color='black'>False<br></font>";
if ($a >= $b) {
}
echo "<font color='blue'>True<br></font>";

} 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

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>";

?>
44

Output:
45

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>"; {

} else { echo "<font


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>";

}
46

Output:
47

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";

?>
48

Output:
49

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";

?>
50

Output:
51

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"

?>
52

Output:
53

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>";

?>

Output:
54

25. WRITE A PROGRAM OF SWITCH


STATEMENT.
55

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;

Output:
56
57

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>";

?>
58

Output:
59

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";

?>
60

Output:

28. WRITE A PROGRAM TO PRINT FIRST 10


NUMBERS USING DO-WHILE LOOP.
61

PROGRAM:

<?php

$number = 1;

do

echo"<center><font size=5><font color=purple>

$number <br>";

$number++;

while ($number <= 10);

?>

Output:
62

29. WRITE A PROGRAM OF FOR EACH LOOP.


63

PROGRAM:

<?php

$cars=array("BMW","Jaguar","Bugatti","Ferrari");

foreach($cars as $value)

echo"<center><font color=purple><font size=50>

$value<br>";

?>
64

Output:
65

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 />";

?>
66

Output:
67

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";

?>
68

Output:
69

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";

?>
70

Output:
71

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";

?>
72

Output:
73

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;

?>
74

Output:
75

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>";

?>
76

Output:
77

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>";

?>
78

Output:
79

37. WRITE A PROGRAM FOR INCLUDE ( )


FUNCTION

PROGRAM:

<?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";

?>
80

Output:
81

38. WRITE A PROGRAM FOR REQUIRE ( )


FUNCTION

PROGRAM:
<?php
<?php

require("Add.php");
$num1 = 10;

echo"<br><font color=blue>"."BCA"; $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;

?>
82

Output:
83

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

?>
84

Output:
85

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;

?>

Output:
86
87

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();

?>

Output:
88
89

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();

?>

Output:
90
91

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);

?>
92

Output:
93

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);

?>
94

Output:
95

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);

?>
96

Output:

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)
97

Output:

Output:
98

47. WRITE A PROGRAM OF ASSOCIATIVE


ARRAY.
99

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/>";

?>

Output:
100

48. WRITE A PROGRAM OF


MULTIDIMENSIONAL ARRAY.
101

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/>";

?> Output:
102

49. WRITE A PROGRAM TO ACCESS THE


ELEMENTS OF AN ARRAY USING SIMPLE
METHOD AND LOOPS.

PROGRAM:
103

<?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>";

?>

Output:
104

50. WRITE A PROGRAM OF ARRAY


FUNCTIONS.
105

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

?>

Output:
106
107

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;

?>

Output:
108

 DOUBLE QUOTED:

PROGRAM:
109

<?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

?>

Output:
110

52. WRITE A PROGRAM OF INTERPOLATION


WITH CURLY BRACES.

PROGRAM:
111

<?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>";

?>

Output:
112

53. WRITE A PROGRAM OF CHARACTERS


AND STRING INDEXES.
113

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;

?>

Output:
114

54. WRITE A PROGRAM OF STRING


OPERATORS.
 CONCATENATION OPERATOR (.)
115

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

?>

Output:
116

 CONCATENATING ASSIGNMENT
OPERATOR (.=)
117

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

?>

Output:
118

55. WRITE A PROGRAM OF STRING


FUNCTIONS.
119

PROGRAM:

<?php $str2 = " Hello World!";


echo "<font color=red>"; echo "Without Trim: $str2 <font
color=blue><br/>";
echo strlen("Hello world")."<font 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", 6)."<font echo strtoupper("hello world")."<font
color=red><br/>"; color=blue><br/>"; // Converts to
uppercase
echo strrev("Hello World")."<font color=blue><br/>";
echo strtolower("HELLO WORLD")."<font
echo strrpos("Hello world Hello world", "wo")."<font
color=red><br/>"; // Converts to
color=blue><br/>";
lowercase

// ucfirst() and ucwords()


// chr() function
echo ucfirst("hello world")."<font
echo chr(65)."<font color=red><br/>"; // Outputs color=blue><br/>"; // Capitalizes first
character 'A' character

echo ucwords("hello world")."<font


color=red><br/>"; // Capitalizes first
// ltrim() and rtrim() functions
character of each word
$str = " Hello World!";

echo "Without Ltrim: $str <font color=blue><br/>";


// wordwrap() function
echo "With Ltrim: ".ltrim($str)."<font
$str3 = "The wordwrap() function wraps a
color=red><br/>";
string into new lines when it reaches a
$str1 = " Hello World"; specific length";

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

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";

?>

Output:
122
123

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>

Output:
124

58. WRITE A PROGRAM OF MULTI LINE TEXT


INPUT CONTROL.
125

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>

Output:
126

59. WRITE A PROGRAM OF CHECKBOX.

PROGRAM:
<html>

<body>

<form name="f1">

<label style="color: red;">


<input type="checkbox" name="DM" value="on">DM

127

Output:
128

60. WRITE A PROGRAM OF RADIO BUTTON.


129

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>

Output:
130

61. WRITE A PROGRAM OF DROP COLUMN


LIST.
131

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

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>
134

Output:
135

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;

?>
136

Output:
137

 $_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>"

?>
138

Output:
139

$_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>
140

Output:
141

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>
142

Output:
143

65. 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>
144

Output:
145

66. 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 />";

?>
146

Output:
147

67. 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>
148

Output:
149

68. 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>
150

Output:
151

69. 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>".($msg); ?>

</body>

</html>
152

Output:
153

70. 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>

Output:
154

71. WRITE A PROGRAM OF SID CONSTANT.


155

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>
156

Output:
157

72. 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>";

?>
158

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy