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

PHP 1

Php

Uploaded by

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

PHP 1

Php

Uploaded by

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

INTRODUCTION

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.

Common uses of PHP

• 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

Five important characteristics make PHP's practical nature possible:

• 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

2. Aim: - Write a PHP program to swap two numbers.

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>

<title> Find out odd or even number. </title>

</head >

<body>

<?php

$a = 10;

if ($a % 2==0)

4
echo "Given number is" . "<br>" . "<b>EVEN<b>" ;

else

echo "Given number is" . "<br>" . "<b>ODD<b>";

?>

</body>

</html>

Output:

Given number is
EVEN

3.2 Aim: - Write a php program to find maximum of three numbers.

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

3.3 Aim:- Program to demonstrate elseif/ else if condition.

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:

4.3 Aim:- PHP program to demonstrate do-while loop.

Procedure:

<html>

<head>

<title>sum of digits</title>

</head>

<body>

<?php

$i=1;

$sum=0;

do

$sum=$sum+$i;

$i++;

} while($i<=5);

echo "sum of digits upto 5"."<br>".$sum;

?>

</body>

</html>

Output:

sum of digits upto 5


15

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:

Given character is vowel

4.5 Aim:- PHP program to demonstrate continue statement.

Procedure:

<html>
<head>
<title> continue statement</title>
</head>
<body>

<?php
//php program to demonstrate the use of continue statement

echo "Even numbers between 1 to 20: </br>";


$i = 1;
while ($i<=20) {
if ($i %2 == 1) {
$i++;
continue; //here it will skip rest of statements
}
echo $i;
echo "</br>";
$i++;
}
?>
12
</body>
</html>

Output:

Even numbers between 1 to 20:


2
4
6
8
10
12
14
16
18
20

4.6 PHP program to print array elements using foreach loop.

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:

Array elements are:


Summer
Winter
Autumn
Rainy

5. Aim: - Write a PHP Program to demonstrate the variable function:


gettype():

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

6. Aim: Write a PHP Program to demonstrate the variable function:


settype()

Procedure:

<html>

<head>

<title> Variable function:Settype:1 </title>


</head>
<body>

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

$val4 = "BetterLuck next time";

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

8. Aim:- Write a PHP program to generate a random number between 1


and 100.

Procedure:

<html>

<head>

<title>random number</title>

</head>

<body>

<?php

// Generating Random numbers without range

// rand() function return the random number

$num1 = rand();

echo "Random number: " . $num1 . "\n";

// Generating Random numbers in given range

$num2 = rand(1, 100);

echo "</br>";

echo "Random number in the range (1, 100): "."<br>". $num2;

?>

</body>

</html>

Output:

Random number: 553354359


Random number in the range (1, 100):
27

18
9. Aim:- Write a program to demonstrate PHP array sorting function.

Procedure:
<html>

<body>

<?php

$numbers = array(40, 61, 2, 22, 13);

sort($numbers);

$arrlength = count($numbers);

echo "Sorted array by ascending order";

echo "<br>";

for($x = 0; $x < $arrlength; $x++) {

echo $numbers[$x];

echo "<br>";

echo "Sorted array by descending order";

echo "<br>";

rsort($numbers);

$arrlength = count($numbers);

for($x = 0; $x < $arrlength; $x++) {

echo $numbers[$x];

echo "<br>";

?>

</body>

</html>

19
Output:

Sorted array by ascending order


2
13
22
40
61
Sorted array by descending order
61
40
22
13
2

10. Aim:- Write a program to demonstrate PHP array function - key


sorting.

Procedure:

<!DOCTYPE html>

<html>

<body>

<?php

$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");

ksort($fruits);

echo "Sorted array in ascending order by key";

echo "</br>";

foreach ($fruits as $key => $val) {

echo "</br>";

echo "$key = $val\n";

20
krsort($fruits);

echo "</br>Sorted array in descending order by key";

echo "</br>";

foreach ($fruits as $key => $val) {

echo "</br>";

echo "$key = $val\n";

?>

</body>

</html>

Output:

Sorted array in ascending order by key

a = orange
b = banana
c = apple
d = lemon
Sorted array in descending order by key

d = lemon
c = apple
b = banana
a = orange

11. Aim:- Write a program to demonstrate PHP array function – value


sorting.

Procedure:

<!DOCTYPE html>

<html>

21
<body>

<?php

$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" =>
"apple");

asort($fruits);

echo "Sorted array in ascending order by value</br>";

foreach ($fruits as $key => $val) {

echo "</br>";

echo "$key = $val\n";

arsort($fruits);

echo "</br>Sorted array in descending order by value";

echo "</br>";

foreach ($fruits as $key => $val) {

echo "</br>";

echo "$key = $val\n";

?>

</body>

</html>
Output:

Sorted array in ascending order by value

c = apple
b = banana
d = lemon
a = orange
22
Sorted array in descending order by value

a = orange
d = lemon
b = banana
c = apple

12. Aim:- Write a program to demonstrate PHP array random sorting.

Procedure:

<!DOCTYPE html>

<html>

<body>

<?php

// Creating an array containing elements

$colours = array("green","red","black","yellow","white","blue");

// Randomize the order of array items

shuffle($colours);

foreach ($colours as $value){

echo "$value" . "<br>";

?>

</body>

</html>
Output:

red
green
white
black

23
blue
yellow

13. Aim:- Write a program to demonstrate PHP array reverse sorting.

Procedure:

<!DOCTYPE html>

<html>

<body>

<?php

// PHP function to illustrate the use of array_reverse()

function Reverse($array)

return(array_reverse($array));

$array = array("ram", "akash", "saran", "mohan");

echo "Array before reverse:</br>";

print_r($array);

echo "</br>Array after reverse:</br>";

print_r(Reverse($array));

?>

</body>

</html>
Output:

Array before reverse:


Array ( [0] => ram [1] => akash [2] => saran [3] => mohan )
Array after reverse:
Array ( [0] => mohan [1] => saran [2] => akash [3] => ram )
24
14. Aim:- Write a program to demonstrate PHP array to string conversion
(Implode function).

Procedure:

<!DOCTYPE html>

<html>

<body>

<!DOCTYPE html>

<html>

<body>

<?php

$arr = array('Hello','World!','Beautiful','Day!');

echo implode(" ",$arr)."<br>";

?>

</body>

</html>

</body>

</html>
Output:

Hello World! Beautiful Day!

15. Aim:- Write a program to demonstrate PHP string to array


conversion.

Procedure:

<!DOCTYPE html>

<html>

<body>

25
<?php

$str = 'Hello world';

$split = str_split($str);

// display result using print_r

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

$arr = array("MAC", "WINDOWS","LINUX", "SOLARIS");

$search = "WINDOWS";

echo "searched element index is :<br>";

echo array_search($search,$arr,true);

echo "<br>array size is</br>";


26
// Printing array size

echo count($arr);

echo "<br>";

?>

</body>

</html>

Output:

searched element index is :


1
array size is
4

17. Aim:- Write a program to demonstrate PHP array replace function.

Procedure:

<!DOCTYPE html>

<html>

<body>

<?php

// Array to be replaced

$array1 = array("orange", "banana", "apple",

"raspberry");

echo " Array to be replaced<br>";

print_r($array1);

echo "<br>";

// arrays that will replace the values

// in the first array

27
$array2 = array(0 => "pineapple", 4 => "cherry");

$array3 = array(0 => "grape");

$resArr = array_replace($array1, $array2,

$array3);

echo "Replaced array elements:<br>";

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

You might also like