WBP File

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

PROGRAM 1

NESTED IF

Aim: to write a php program to show the usage of nested if statement


Program:
<!DOCTYPE html>
<html>
<body>
<?php
$grade=75;
if($grade>=60)
{
echo"You passed the exam.<br>";

if($grade>=90)
{
echo"Congratulations! You got an A.";
}
elseif($grade>=80)
{
echo"Congratulations! You got a B.";
}
elseif($grade>=70)
{
echo"Good!You got a C.";
}
}
else
{
echo"Sorry you have failed.";
}
?>
</body>
</html>

1
OUTPUT

2
PROGRAM 2
TYPE CASTING

Aim:Write a program to demonstrate type casting


Program:
<!DOCTYPE html>
<html>
<head>
<title>Type Casting</title>
<style>
h1{
background-color:yellow
text-align:center
}
</style>
</head>
<body>
<h1>
<?php
//integer
$v1=42;
echo"<br>Original integer:<br>";
var_dump($v1);
echo "<p>Type:" .gettype($v1)."</p>";

//float
$v2=6.99;
echo"<br>Original float:<br>";
var_dump($v2);
echo "<p>Type:" .gettype($v2)."</p>";

//string
$var="Hello,PHP!";
echo"<br>Original string:<br>";
var_dump($var);
echo "<p>Type:" .gettype($var)."</p>";

//Boolean
$v3="true";
echo"<br>Original boolean:<br>";
var_dump($v3);

3
echo "<p>Type:" .gettype($v3)."</p>";

//array
$v3="true";
echo"<br>Original boolean:<br>";
var_dump($v3);
echo "<p>Type:" .gettype($v3)."</p>";
?>
</h1>
</body>
</html>

4
OUTPUT

5
PROGRAM 3
SWITCH CASE

Aim: Write a program to show the usage switch case using menu driven program
Program:
<html><body>

<H1><center> SIMPLE CALCULATOR </center></H1>

<form name=f1 method="post" action="3rdprog.php">


First number : <input type="text" name="txtn1"><br><br>
Second number : <input type="text" name="txtn2"><br><br>
Choice opted (+,-,*,/): <input type="text" name="txtopt"><br><br>
<input type="submit" value="submit"><br><br>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"]=="POST"){
$a=$_REQUEST["txtopt"];
$b=$_POST["txtn1"];
$c=$_POST["txtn2"];
switch ($a){
case"+":
$d=($b+$c);
echo"Addition is ".$d;
break;
case"-":
$d=($b-$c);
echo"Subtraction is ".$d;
break;
case"/":
$d=($b/$c);
echo"Division is ".$d;
break;
default:
echo"Multiplication is ".($b*$c);
}
}
?>
</body>
</html>

6
OUTPUT

7
PROGRAM 4
For/While/Do While Loop

Aim: Write a program to show the usage of for/while/do while loop coding
Program:
<?php
echo("while_loop=");
echo("<br>");
$x=1;
while($x<=5)
{
echo"The number is:$x"."<br>";
$x++;
}
echo("<br>");
echo("do_while_loop=");
echo("<br>");
$y=6;
do{
echo"The number is:$y"."<br>";
$y++;
} while($y<=10);
echo("<br>");
echo("for_loop=");
echo("<br>");

for ($z=11;$z<=15;$z++)
{
echo"The number is:$z"."<br>";
}
?>

8
OUTPUT

9
PROGRAM 5
ASSOCIATIVE ARRAY

Aim: Example for Associative Array –for each


Program:
<html>
<body>
<h1><center> Associative Array</center></h1>
<?php
$a=array(10,20,30);
echo "Example for for loop<br>";
for($i=0;$i<count($a);$i++)
{
echo "value is" .$a[$i]. "<br>";
}
$b=array("name"=>"sai","age"=>25,"salary"=>25000);
echo"Example for foreach";
foreach ($b as $emp => $de)
{
echo "$emp => $de"."<br>";
}
?>
</body>
</html>

10
OUTPUT

11
PROGRAM 6
MULTI-DIMENSIONAL ARRAY

Aim: Write a program to use multi – dimensional ararys


Program:
<html>
<body>
<h1><center> Multi dimensional Array </center></h1>
<?php
$employee = array(
"sairam"=>array("age"=>25,"designation"=>"sr.Admin","work_expr"=>4),
"bala"=>array("age"=>24,"designation"=>"SW ENG","work_expr"=>2),
"sumathy"=>array("age"=>22,"designation"=>"Developer","work_expr"=>6),
"ansh"=>array("age"=>20,"designation"=>"Trainer","work_expr"=>5)
);
echo"<br>";
echo"<br>";
echo"<br>";
foreach ($employee as $name => $details)
{
echo "Name is " .$name."<br>";
echo "Age is " .$details["age"]."<br>";
echo "Designation is " .$details["designation"]."<br>";
echo "Experience is " .$details["work_expr"]."<br>";
echo"<br>";
echo"<br>";
}
?>
</body>
</html>

12
OUTPUT

13
PROGRAM 7
SORTING

Aim: Write a program to perform all four types of sorting coding


Program:
<?php
$a1=array (6,8,3,2,9);
sort($a1);
echo("sort=");
print_r($a1)."<br>";
echo("<br>");
echo("<br>");

$a2=array(6,8,3,2,9);
rsort($a2);
echo("rsort=");
print_r($a2)."<br>";
echo("<br>");
echo("<br>");

$a3=array("sonam"=>"45","ram"=>"25","priya"=>"11","ira"=>"56");
asort($a3);
echo("asort=");
print_r($a3)."<br>";
echo("<br>");
echo("<br>");

$a4=array("45"=>"sonam","25"=>"ram","11"=>"priya","56"=>"ira");
ksort($a4);
echo("ksort=");
print_r($a4)."<br>";
echo("<br>");
echo("<br>");
?>

14
OUTPUT

15
PROGRAM 8
ARRAY FUNCTIONS

Aim: Write a program to implement Arraypad(),array_slice(),array_splice(),list()functions.


Program:
<html>
<head>
<title>Array Functions</title>
</head>
<body>
<?php
echo "<b> array_pad </b><br>";
$array = array(10, 25, 30);
$paddedArray = array_pad($array, 4, 45);
print_r($paddedArray);

echo "<br><br><br>";
echo "<b> array_slice </b><br>";
$array = array(1, 2, 3, 4, 5);
$slicedArray = array_slice($array, 2, 3);
print_r($slicedArray);
echo "<br><br><br>";

echo "<b> array_splice </b><br>";


$array = array(1, 2, 3, 4, 5);
$removedElements = array_splice($array, 1, 2);
echo "Modified Array: ";
print_r($array);
echo "<br>";
echo "Removed Elements: ";
print_r($removedElements);
echo "<br><br><br>";

echo "<b>Example for List </b><br>";


$info = array('John', 'Doe', '30');
list($firstName, $lastName, $age) = $info;
echo "First Name: $firstName, Last Name: $lastName, Age: $age";

$array = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h');


// Split the array into chunks of size 3
$chunks = array_chunk($array, 3);

16
// Display each chunk
foreach ($chunks as $ch) {
print_r($ch);
echo "<br>";
}
?>
</body>
</html>

17
OUTPUT

18
PROGRAM 9
USER DEFINED FUNCTIONS

Aim: Write a program to show the application of user defined functions


Program:

<?php
function rect($l,$b)
{
echo "AREA OF RECTANGLE IS: ".$l*$b."<br>";
}
rect(4,5);

function cir($r)
{
echo "AREA OF CIRCLE IS: ".(22/7)*$r*$r."<br>";
}
cir(7);
?>

19
OUTPUT

20
PROGRAM 10
Program Controlling Functions

Aim : Write a program that Passes control to another page (include, require, exit and die
functions)
Program: EXIT OR DIE
<?php
$age = 1;
if ($age < 2) {
exit("Age must be more than 2");
// This line will not be executed because exit terminates the script
echo "Thank you for running the program";
} else {
echo "Your age is " . $age;
}
?>
 REQUIRE (sample.php)
<html>
<body bgcolor="Lavender">
<h1> Example for REQUIRE </h1>

<?php
$a=10;
$b=20;
?>

</body>
</html>
 REQUIRE CODE
<html>
<body>
<?php
require 'sam.php';
echo "<br>";
echo "Program is working with a value $a and b value $b <br>";
echo "Addition of 2 numbers is " . ($a + $b);
?>
</body>
</html>

21
 INCLUDE
<html>
<body bgcolor="Lavender">
<h1>INCLUDE</h1>
</body>
</html>

 INCLUDE CODE
<html>
<body>
<?php
include 'sam.php';
echo "<br>";

echo "Program is working with a value $a and b value $b <br>";


echo "Addition of 2 numbers is " . ($a + $b);
?>
</body>
</html>

22
OUTPUT

23
PROGRAM 11
Post,Request,Get Method

Aim: Usage of post,request and get method


Program:
<!DOCTYPE html>
<html>
<head>
<title>Input Form</title>
</head>
<body>

<form method="post" action="process.php">


<label for="input1">Input 1:</label>
<input type="text" name="input1" id="input1" required><br>
<label for="input2">Input 2:</label>
<input type="text" name="input2" id="input2" required><br>
<label for="input3">Input 3:</label>
<input type="text" name="input3" id="input3" required><br>
<input type="submit" value="Submit">
</form>

</body>
</html>

PROCESS.PHP
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve input values from the form
$input1 = $_POST["input1"];
$input2 = $_POST["input2"];
$input3 = $_POST["input3"];

// Display the inputs


echo "Input 1: " . $input1 . "<br>";
echo "Input 2: " . $input2 . "<br>";
echo "Input 3: " . $input3 . "<br>";
}
?>

24
OUTPUT

25

You might also like