Practical of Php

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

INDEX

SR.NO PRACTICAL NAME SIGNATURE

1 Write a program in PHP to demonstrate looping statements.

2 Write a program in PHP to demonstrate conditional statements.

3 Write a program in PHP to Create an Array, Insert elements in Array,


Accessing Elements from Array and Displaying elements of Arrays.

4 Write a program in PHP to demonstrate including multiple files in PHP


webpage.

5 Write a program in PHP to Creating and Calling Your Own Functions.

6 Write a program in PHP to declare a class, creating an Object,


demonstrates Writing Methods & Declaring Properties, Accessing
Objects.
7 Write a program in PHP to demonstrate String Functions.

8 Write a program in PHP to create/design a User Registration Form,


validate form data and display entered form data on webpage.

9 Use MYSQL in command line mode for some operations.

10 Write a program in PHP to Connecting to MySQL and Selecting the


Database ,Executing Simple Queries, and Retrieving Query Results.
Practical No. 1
Aim: Write a Program in PHP to demonstrate looping statement in PHP.

1.While loop

Code:

<?php

$x=1;

while($x<=5)

echo"the number is: $x<br>";

$x++;

?>

Output:
2.Do-while Loop

Code:

<?php

$x=5;

do

echo"the number is: $x<br>";

$x--;

while($x>=1);

?>

Output:
3. For loop

Code:

<?php

for($x=0; $x<=7; $x++)

echo"the number is: $x<br>";

?>

Output:
Practical No. 2
Aim: Write a program in PHP to demonstrate conditional statement in PHP.

1.IF Condition

Code:
<?php

$x=07;

if($x=07)

echo"Thala for a reason: $x<br>";

?>

Output:
2.If-else Condition

Code:

<?php

$x=8;

if($x%2==0)

echo"the number is even number.<br>";

else

echo"the number is odd number<br>";

?>

Output:
3.Else-if Condition.
Code:
<?php

// Define the student's grade

$grade = 85;

// Check the grade and print out the corresponding message

if ($grade >= 90) {

echo "Grade: A - Excellent!";

} elseif ($grade >= 80) {

echo "Grade: B - Good job!";

} elseif ($grade >= 70) {

echo "Grade: C - Fair performance.";

} elseif ($grade >= 60) {

echo "Grade: D - Needs improvement.";

} else {

echo "Grade: F - Failed. Better luck next time.";

?>

Output:
Practical No. 3
Aim: Write a program in PHP to Create an Array, Insert elements in Array,
Accessing Elements from Array and Displaying elements of Arrays.

Code:
<?php

// Step 1: Create an array

$array = [];

// Step 2: Insert elements into the array

$array[] = "Apple";

$array[] = "Banana";

$array[] = "Cherry";

$array[] = "Date";

// Step 3: Access elements from the array

$firstElement = $array[0]; // Accessing the first element

$secondElement = $array[1]; // Accessing the second element

// Step 4: Displaying elements of the array

echo "Array Elements:<br>";

foreach ($array as $index => $value) {

echo "Element at index $index: $value<br>";

// Displaying accessed elements

echo"<br>";

echo "Accessed Elements:<br>";

echo "First Element: $firstElement<br>";

echo "Second Element: $secondElement<br>";

?>
Output:
Practical No. 4
Aim: Write a program in PHP to demonstrate including multiple files
in PHP webpage.

Code:
<?php
include("for.php");
echo"<br>";
include("while.php");
echo"<br>";
include("dowhile.php");
echo"<br>";
include("condition.php");
echo"<br>";
include("ifelse.php");
echo"<br>";
echo"file is included";
echo"<br>";
?>

Output:
Practical No. 5

Aim: Write a program in PHP to Creating and calling your own


function.

Code:
<?php

function calculateArea($Val1, $Val2) {


return $Val1 * $Val2;
}

function displayArea($Val1, $Val2) {


$Multiplicate = calculateArea($Val1, $Val2);
echo "The multiplication of the two numbers value 1 $Val1 and value 2 $Val2 is:
$Multiplicate .";
}

// Calling the function with example values


$Val1 = 5;
$Val2 = 10;

displayArea($Val1, $Val2);

?>
OUTPUT:
Practical No. 7
Aim: Write a program to demonstrate String Function.

1. Get the length of String.

Code:

<?php
echo strlen ("Hello world");
?>

Output:

2. Count the number of the words in String.

Code:

<?php
echo str_word_count ("Hello World");
?>

OUTPUT:
3. Reverse a String.

Code:

<?php
echo strrev ("MS Dhoni");
?>

OUTPUT:

4. Search for Specific text within a String.

Code:

<?php
echo strpos ("Kabhi kabhi lagta hai apan hi bhagwan hai","bhagwan");
?>

OUTPUT:
5. Replace text within a String.

Code:

<?php
echo str_replace ("Bhai","Sir","GM Bhai");
?>

OUTPUT:

You might also like