PHP 1
PHP 1
PHP is a recursive acronym for "PHP: Hypertext preprocessor". Rasmus Lerdorf unleashed the first version
of PHP way back in 1994. PHP is a server-side scripting language that is embedded in HTML. PHP is free
to download and use. It is used to manage dynamic content, databases, session tracking, even build entire
e-commerce sites. It is integrated with a number of popular databases, including MySQL, PostgreSQL,
Oracle, Sybase, Informix, and Microsoft SQL Server. PHP supports a large number of major protocols such
as POP3, IMAP, and LDAP. PHP4 added support for Java and distributed object architectures (COM and
CORBA), making n-tier development a possibility for the first time.
• PHP performs system functions, i.e. from files on a system it can create, open, read, write, and
close them.
• PHP can handle forms, i.e. gather data from files, save data to a file, through email you can send
data, return data to the user.
• You add, delete, modify elements within your database through PHP.
• Access cookies variables and set cookies.
• Using PHP, you can restrict users to access some pages of your website.
• It can encrypt data.
Characteristics of PHP
• Simplicity
• Efficiency
• Security
• Flexibility
• Familiarity
2
1.Aim: Write a PHP program to print “Hello World”.
Procedure:
<html>
<head>
<title> My Simple Program </title>
</head>
<body>
<?php
echo "Hello World";
?>
</body>
</html>
Output:
Hello World
Procedure:
<html>
<head>
<title>
Swapping of two numbers.
</title>
</head>
<Body>
<?php
$a = 10;
$b = 20;
echo “<b>Before swapping</b>”. “<br>”;
echo "a = $a"."<br>"."b = $b"."<br>";
$a = $a + $b;
$b = $a - $b;
$a = $a - $b;
3
echo "<b>After Swapping"."<br>"." a = $a"."<br>"."b = $b<b>";
?>
</body>
</html>
Output:
Before swapping
a =10
b =20
After Swapping
a=20
b=10
3. Programs involving various control structures like if, if-else, elseif/else if.
3.1 Aim: - Write a php program to find odd or even number from given number.
Procedure:
<html>
<head>
</head >
<body>
<?php
$a = 10;
if ($a % 2==0)
4
echo "Given number is" . "<br>" . "<b>EVEN<b>" ;
else
?>
</body>
</html>
Output:
Given number is
EVEN
Procedure:
<html>
<head>
<title> Max out of three </title>
</head>
<body>
<?php
$a = 1;
$b = 4;
$c=3;
if($a>$b)
{
if($a > $c)
echo “Maximum num a=$a”;
else
echo "Maximum num c = $c";
}
else
5
{
if($c > $b)
echo "Maximum num c = $c";
else
echo "Maximum num b = $b";
}
?>
</body>
</html>
Output:
Maximum num b =4
Procedure:
<html>
<head>
<title> compare values of two numbers</title>
</head>
<body>
<?php
$x=5;
$y=10;
if ($x > $y)
{
echo "x is bigger than y";
}
elseif ($x == $y)
{
echo "x is equal to y";
}
else
{
echo "x is smaller than y";
}
?>
</body>
</html>
6
Output:
x is smaller than y
7
4. Programs involving various control structures like while, do-while, for,
foreach, switch, break, continue etc.
4.1 Aim:- Write a PHP program to find factorial of a number using for
loop.
Procedure:
<html>
<head>
<title>Factorial Program using loop in PHP</title>
</head>
<body>
<form method="post">
Enter the Number:<br>
<input type="number" name="number" >
<input type="submit" name="submit" value="Submit" />
</form>
<?php
if($_POST){
$fact = 1;
//getting value from input text box 'number'
$number = $_POST['number'];
echo "Factorial of $number:<br><br>";
//start loop
for ($i = 1; $i <= $number; $i++){
$fact = $fact * $i;
}
echo $fact . "<br>";
}
?>
</body>
</html>
Output:
4.2 Aim:- Program to check whether the given number is Armstrong or not
using while loop.
8
Procedure:
<html>
<body>
<form method="post">
Enter the Number:
<input type="number" name="number">
<input type="submit" value="Submit">
</form>
</body>
</html>
<?php
if($_POST)
{
//get the number entered
$number = $_POST['number'];
//store entered number in a variable
$a = $number;
$sum = 0;
//run loop till the quotient is 0
while( $a != 0 )
{
$rem = $a % 10; //find reminder
$sum = $sum + ( $rem * $rem * $rem ); //cube the reminder and add it to
the sum variable till the loop ends
$a = $a / 10; //find quotient. if 0 then loop again
}
//if the entered number and $sum value matches then it is an armstrong
number
if( $number == $sum )
{
echo "Yes $number an Armstrong Number";
}else
{
echo "$number is not an Armstrong Number";
}
}
?>
9
Output:
Procedure:
<html>
<head>
<title>sum of digits</title>
</head>
<body>
<?php
$i=1;
$sum=0;
do
$sum=$sum+$i;
$i++;
} while($i<=5);
?>
</body>
</html>
Output:
10
4.4 Aim:- PHP program to demonstrate switch case and break statement
inside.
Procedure:
<html>
<head>
<title>switch case</title>
</head>
<body>
<?php
$ch = 'U';
switch ($ch)
{
case 'a':
echo "Given character is vowel";
break;
case 'e':
echo "Given character is vowel";
break;
case 'i':
echo "Given character is vowel";
break;
case 'o':
echo "Given character is vowel";
break;
case 'u':
echo "Given character is vowel";
break;
case 'A':
echo "Given character is vowel";
break;
case 'E':
echo "Given character is vowel";
break;
case 'I':
echo "Given character is vowel";
break;
case 'O':
echo "Given character is vowel";
11
break;
case 'U':
echo "Given character is vowel";
break;
default:
echo "Given character is consonant";
break;
}
?>
</body>
</html>
Output:
Procedure:
<html>
<head>
<title> continue statement</title>
</head>
<body>
<?php
//php program to demonstrate the use of continue statement
Output:
Procedure:
<html>
<head>
<title>foreach example</title>
</head>
<body>
<?php
//declare array
$season = array ("Summer", "Winter", "Autumn", "Rainy");
echo "Array elements are:"."<br>" ;
//access array elements using foreach loop
foreach ($season as $element) {
echo "$element";
echo "</br>";
}
?>
</body>
13
</html>
Output:
Procedure:
<html>
<head>
<title>
Variable function:gettype:1
</title>
</head>
<body>
<?php
$no = 5;
$value = 13.5;
$name = "Bipin Prajapati";
$var = true;
$myarray= array(110,45," Bipin",1.5,true);
echo gettype($no)."<br/>";
echo gettype($value)."<br/>";
echo gettype($name)."<br/>";
echo gettype($var)."<br/>";
echo gettype($myarray)."<br/>";
?>
</body>
</html>
14
Output:
integer
double
string
boolean
array
Procedure:
<html>
<head>
<?php
$var1 ="anil";
$var3 = "1Bipin";
$var4 = "anil1";
$var2 = true;
settype($var1, "integer");
settype($var2, "string");
settype($var3, "integer");
settype($var4, "integer");
echo gettype($var1)."</br>";
15
echo gettype($var2)."</br>";
echo gettype($var3)."</br>";
echo gettype($var4)."</br>";
echo $var1."</br>";
echo $var2."</br>";
echo $var3."</br>";
echo $var4."</br>";
?>
</body>
</html>
Output:
integer
string
integer
integer
1
1
0
7. Aim:- Program to demonstrate the variable function: var_dump().
Procedure:
<html>
<head>
<title>
16
Variable Function:var_dump()
</title>
</head>
<body>
<?php
$val1 = 678;
$val2 = "a678";
$val3 = "678";
$val5 = 698.99;
$val6 = +125689.66;
echo var_dump($val1)."<br>";
echo var_dump($val2)."<br>";
echo var_dump($val3)."<br>";
echo var_dump($val4)."<br>";
echo var_dump($val5)."<br>";
echo var_dump($val6)."<br>";
?>
</body>
</html>
Output:
int(678)
string(4) "a678"
string(3) "678"
string(20) "BetterLuck next time"
17
float(698.99)
float(125689.66)
Procedure:
<html>
<head>
<title>random number</title>
</head>
<body>
<?php
$num1 = rand();
echo "</br>";
?>
</body>
</html>
Output:
18
9. Aim:- Write a program to demonstrate PHP array sorting function.
Procedure:
<html>
<body>
<?php
sort($numbers);
$arrlength = count($numbers);
echo "<br>";
echo $numbers[$x];
echo "<br>";
echo "<br>";
rsort($numbers);
$arrlength = count($numbers);
echo $numbers[$x];
echo "<br>";
?>
</body>
</html>
19
Output:
Procedure:
<!DOCTYPE html>
<html>
<body>
<?php
ksort($fruits);
echo "</br>";
echo "</br>";
20
krsort($fruits);
echo "</br>";
echo "</br>";
?>
</body>
</html>
Output:
a = orange
b = banana
c = apple
d = lemon
Sorted array in descending order by key
d = lemon
c = apple
b = banana
a = orange
Procedure:
<!DOCTYPE html>
<html>
21
<body>
<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" =>
"apple");
asort($fruits);
echo "</br>";
arsort($fruits);
echo "</br>";
echo "</br>";
?>
</body>
</html>
Output:
c = apple
b = banana
d = lemon
a = orange
22
Sorted array in descending order by value
a = orange
d = lemon
b = banana
c = apple
Procedure:
<!DOCTYPE html>
<html>
<body>
<?php
$colours = array("green","red","black","yellow","white","blue");
shuffle($colours);
?>
</body>
</html>
Output:
red
green
white
black
23
blue
yellow
Procedure:
<!DOCTYPE html>
<html>
<body>
<?php
function Reverse($array)
return(array_reverse($array));
print_r($array);
print_r(Reverse($array));
?>
</body>
</html>
Output:
Procedure:
<!DOCTYPE html>
<html>
<body>
<!DOCTYPE html>
<html>
<body>
<?php
$arr = array('Hello','World!','Beautiful','Day!');
?>
</body>
</html>
</body>
</html>
Output:
Procedure:
<!DOCTYPE html>
<html>
<body>
25
<?php
$split = str_split($str);
print_r($split);
?>
</body>
</html>
</body>
</html>
Output:
Array ( [0] => H [1] => e [2] => l [3] => l [4] => o [5] => [6] => w [7] => o [8] => r [9]
=> l [10] => d )
16. Aim:- Write a program to demonstrate PHP array count and array
search.
Procedure:
<!DOCTYPE html>
<html>
<body>
<?php
$search = "WINDOWS";
echo array_search($search,$arr,true);
echo count($arr);
echo "<br>";
?>
</body>
</html>
Output:
Procedure:
<!DOCTYPE html>
<html>
<body>
<?php
// Array to be replaced
"raspberry");
print_r($array1);
echo "<br>";
27
$array2 = array(0 => "pineapple", 4 => "cherry");
$array3);
print_r($resArr);
?>
</body>
</html>
Output:
Array to be replaced
Array ( [0] => orange [1] => banana [2] => apple [3] => raspberry )
Replaced array elements:
Array ( [0] => grape [1] => banana [2] => apple [3] => raspberry [4] => cherry )
28
29