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

Php Lab Manual

The document provides an overview of PHP, including its definition, capabilities, and installation methods. It contains detailed algorithms and source code for various PHP scripts, such as determining odd/even numbers, calculating the sum of digits, generating Fibonacci series, and checking for Armstrong numbers. Each script includes a description of its aim, algorithm, source code, output, and result of execution.

Uploaded by

Jeya Perumal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Php Lab Manual

The document provides an overview of PHP, including its definition, capabilities, and installation methods. It contains detailed algorithms and source code for various PHP scripts, such as determining odd/even numbers, calculating the sum of digits, generating Fibonacci series, and checking for Armstrong numbers. Each script includes a description of its aim, algorithm, source code, output, and result of execution.

Uploaded by

Jeya Perumal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

CONTENT

S.NO DATE PROGRAM PG.NO SIGN

1 ODD OR EVEN NUMBERS

2 SUM OF DIGITS

3 PRIME NUMBERS

4 MULTIPLICATION TABLE

5 FACTORIAL

6 REVERSE THE NUMBERS

7 FIBONACCI SERIES

8 ARMSTRONG NUMBER

9 BIGGEST NUMBER

10 ARITHMETIC OPERATIONS

11 PALINDROME

12 LEAP YEAR
CLASSIFIES THEIR AGE AS A
13 MINOR, ADULT, OR SENIOR
CITIZEN

14 SWAPPING TWO NUMBERS

AREA AND PERIMETER OF A


15
RECTANGLE

16 COMPARE TWO STRINGS


INTRODUCTION
What is PHP?
 PHP is an acronym for "PHP: Hypertext Preprocessor"
 PHP is a widely-used, open source scripting language
 PHP scripts are executed on the server
 PHP is free to download and use
 PHP was created by romsmus lerdarf in 1994 but appeared in the marked in
1995
What is a PHP File?
 PHP files can contain text, HTML, CSS, JavaScript, and PHP code
 PHP code is executed on the server, and the result is returned to the browser as
plain HTML
 PHP files have extension ".php".
What Can PHP Do?
 PHP can generate dynamic page content
 PHP can create, open, read, write, delete, and close files on the server
 PHP can collect form data
 PHP can send and receive cookies
 PHP can add, delete, modify data in your database
 PHP can be used to control user-access
 PHP can encrypt data.
Why PHP?
 PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
 PHP is compatible with almost all servers used today (Apache, IIS, etc.)
 PHP supports a wide range of databases
 PHP is free. Download it from the official PHP resource: www.php.net
 PHP is easy to learn and runs efficiently on the server side
What's new in PHP 7
 PHP 7 is much faster than the previous popular stable release (PHP 5.6)
 PHP 7 has improved Error Handling
 PHP 7 supports stricter Type Declarations for function arguments
 PHP 7 supports new operators (like the spaceship operator: <=>)
PHP Installation
To start using PHP, you can:
 Find a web host with PHP and MySQL support
 Install a web server on your own PC, and then install PHP and MySQL .
Use a Web Host with PHP Support
 If your server has activated support for PHP you do not need to do anything.
 Just create some .php files, place them in your web directory, and the server will
automatically parse them for you.
 You do not need to compile anything or install any extra tools.
 Because PHP is free, most web hosts offer PHP support

Set Up PHP on Your Own PC


However, if your server does not support PHP, you must:
 install a web server
 install PHP
 install a database, such as MySQL
ODD OR EVEN NUMBERS

AIM:
To develop a PHP script that determines whether a given number is odd or even.

ALGORITHM:
1) Start the program.
2) Accept a number as input.
3) Check if the number is divisible by 2:
 If number % 2 == 0, it is even
 Otherwise, it is odd
4) Display the output.
5) End the program
SOURCE CODE:
<?php
$number = 1;

if ($number % 2 == 0) {
echo "$number is an even number";
} else {
echo "$number is an odd number";
}

?>

OUTPUT:
1 is an odd number

RESULT: Thus the program has been successfully executed.


SUM OF DIGITS

AIM:
To develop a PHP script that calculates the sum of the digits of a given number.

ALGORITHM:
1) Start the program.
2) Initialize num with a given number (e.g., 14597).
i)Set sum = 0 to store the sum of digits.
ii)Repeat the following steps until num becomes 0:
o Extract the last digit using num % 10 and store it in rem.
o Add rem to sum.
o Remove the last digit by performing integer division (num =
floor(num / 10)).
3) Print the sum of digits.
4) End the program.
SOURCE CODE:
<?php
$num = 14597;
$sum = 0;
$rem = 0;
for ($i = 0; $num > 0; $i++)
{
$rem = $num % 10;
$sum = $sum + $rem;
$num = floor($num / 10);
}
echo "Sum of digits of 14597 is $sum";
?>

OUTPUT:
Sum of digits of 14597 is 26

RESULT: Thus the program has been successfully executed.


PRIME NUMBERS

AIM:
To develop a PHP script that prints the first 15 prime numbers.

ALGORITHM:
1) Start the program.
2) Initialize count = 0 to track the number of prime numbers found.
3) Set num = 2 as the first number to check.
4) Use a while loop to continue until 15 prime numbers are found (count < 15).
 Set div_count = 0 to count the number of divisors of num.
 Use a for loop to iterate from 1 to num and check how many times num is
divisible.
 If num has exactly two divisors (1 and itself), it is a prime number:
o Print num.
o Increment count.
5) End the program.
SOURCE CODE:
<?php
$count = 0;
$num = 2;
while ($count < 15)
{
$div_count = 0;
for ($i = 1; $i <= $num; $i++)
{
if (($num % $i) == 0)
{
$div_count++;
}
}
if ($div_count == 2)
{
echo $num . ", ";
$count++;
}
$num++;
}
?>

OUTPUT:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,

RESULT: Thus the program has been successfully executed.


MULTIPLICATION TABLE

AIM:
To develop a PHP script that prints the multiplication table of a given number, in
this case, the number 7.

ALGORITHM:
1) Start the program.
2) Define a constant a = 7 (the number for which the multiplication table will be
printed).
3) Print a message to indicate the start of the table (echo "Print the table:").
4) Use a for loop to iterate from i = 1 to i = 10.
 In each iteration, multiply i by the constant a (7) and print the result.
5) End the program.
SOURCE CODE:
<?php
define('a',7);
echo"Print the Multiplication table:".PHP_EOL;
for($i=1;$i<=10;$i++)
{
echo$i*a.PHP_EOL;
}

?>

OUTPUT:
Print the Multiplication table:
7
14
21
28
35
42
49
56
63
70

RESULT: Thus the program has been successfully executed.


FACTORIAL

AIM:
To develop a PHP script that calculates the factorial of a given number.

ALGORITHM:
1) Start the program.
2) Initialize the variable num = 4 (the number for which the factorial will be
calculated).
3) Initialize factorial = 1 (since the factorial starts at 1).
4) Use a for loop to iterate from num down to 2 (i.e., x = $num; $x > 1; $x--).
 In each iteration, multiply factorial by the current value of x.
5) After the loop, the result will be stored in the factorial variable.
6) Print the result as the factorial of the given number.
7) End the program.
SOURCE CODE:
<?php
$num=4;
$factorial=1;
for($x= $num;$x>1;$x--)
{
$factorial=$factorial*$x;
}
echo"factorial of $num is$factorial";

?>

OUTPUT:
factorial of 4 is24

RESULT: Thus the program has been successfully executed.


REVERSE NUMBER

AIM:
To develop a PHP script that reverses a given number (in this case, 23456).

ALGORITHM:
1) Start the program.
2) Initialize num = 23456 (the number to be reversed) and revnum = 0 (the
variable to store the reversed number).
3) Use a while loop that continues as long as num > 0.
 Extract the last digit of the number using num % 10 and store it in rem.
 Update revnum by shifting its current value to the left (multiply by 10) and
adding rem.
 Remove the last digit from num by dividing it by 10 and using floor() to
ensure it’s an integer.
4) Continue the loop until all digits have been processed.
5) Print the reversed number.
6) End the program.
SOURCE CODE:
<?php
$num=23456;
$revnum=0;
while($num>0)
{
$rem=$num%10;
$revnum=($revnum*10)+$rem;
$num=floor($num/10);
}
echo"Reverse of number 23456 is:$revnum";
?>

OUTPUT:
Reverse of number 23456 is:65432

RESULT: Thus the program has been successfully executed.


FIBONACCI SERIES

AIM:
To develop a PHP script that generates the Fibonacci series up to the n-th term.

ALGORITHM:
1) Start the program.
2) Define a function Fibonacci Series($n) that accepts a parameter $n (the
number of terms to generate in the Fibonacci series).
3) Initialize num1 = 0 and num2 = 1 (the first two numbers of the Fibonacci
sequence).
4) Use a for loop to iterate $n times and generate the Fibonacci numbers:
 In each iteration, print the current value of num1.
 Compute the next Fibonacci number as num3 = num1 + num2.
 Update num1 to the previous value of num2, and num2 to the new num3.
5) Call the function with $n = 10 to display the first 10 Fibonacci numbers.
6) End the program.
SOURCE CODE:
<?php
function fibonacciSeries($n)
{
$num1=0;
$num2=1;
for($i=0;$i<$n;$i++)
{
echo$num1.",";
$num3 = $num1+$num2;
$num1=$num2;
$num2=$num3;
}
}
$n=10;
fibonacciSeries($n);
?>

OUTPUT:
0,1,1,2,3,5,8,13,21,34,

RESULT:
Thus the program to Fibonacci series has been successfully executed.
ARMSTRONG NUMBER

AIM:
To develop a PHP script that checks if a given number is an Armstrong number
(also known as a narcissistic number).

ALGORITHM:
1) Start the program.
2) Define a function armstrongCheck($number) that accepts a number to check.
3) Initialize sum = 0 to accumulate the sum of the digits raised to the power of
the number of digits.
4) Convert the number to a string and find the total number of digits (digits =
strlen((string)$number)).
5) Use a while loop to process each digit of the number:
o Extract the last digit using temp % 10.
o Raise the digit to the power of the number of digits (pow($digit,
$digits)) and add it to sum.
o Remove the last digit using integer division (temp = (int)($temp /
10)).
6) After the loop, compare if sum is equal to the original number. If it is, the
number is an Armstrong number.
7) Return true if the number is an Armstrong number, otherwise false.
8) Print the result based on the return value.
9) End the program.
SOURCE CODE:
<?php
function isArmstrong($num)
{
$sum = 0;
$temp = $num;
$digits = strlen($num);

while ($temp)
{
$sum += pow($temp % 10, $digits);
$temp = (int)($temp / 10);
}

return $sum == $num;


}

$number = 153;
echo isArmstrong($number) ? "$number is an Armstrong number." : "$number
is not an Armstrong number.";
?>
OUTPUT:
153 is an Armstrong number.

RESULT: Thus the program has been successfully executed.


BIGGEST NUMBER

AIM:
To develop a PHP script that finds the biggest number among three given
numbers.

ALGORITHM:
1) Start the program.
2) Initialize three numbers: number1, number2, and number3.
3) Create an array numbers containing the three numbers.
4) Sort the array in ascending order using the sort() function.
5) After sorting, the largest number will be at the last index ($numbers[2]).
6) Display the largest number.
7) End the program.
SOURCE CODE:
<?php
$number1 = 12;
$number2 = 7;
$number3 = 15;
$numbers = [$number1, $number2, $number3];
sort($numbers);
echo "The largest number among $number1, $number2, and $number3 is: " .
$numbers[2] . "\n";
?>

OUTPUT:
The largest number among 12, 7, and 15 is: 15

RESULT: Thus the program has been successfully executed.


ARITHMETIC OPERATIONS

AIM:
To develop a PHP script that performs and displays the result of various arithmetic
operations (addition, subtraction, multiplication, division, exponentiation, and
modulus) on two numbers.

ALGORITHM:
1) Start the program.
2) Define two variables x and y with values for the arithmetic operations
(e.g., x = 10 and y = 4).
3) Display a title for the arithmetic operations.
4) Perform and display the following operations:
 Addition: x + y
 Subtraction: x - y
 Multiplication: x * y
 Division: x / y
 Exponentiation: x ** y
 Modulus: x % y
5) Display the results of each operation in a readable format.
6) End the program.
SOURCE CODE:
<?php
echo"Arithmetic operations:\n";
echo"______________________:";
echo"\n";
$x=10;
$y=4;
echo"\n Addition:".($x+$y);
echo"\n Subtraction:".($x-$y);
echo"\n Multiplications:".($x*$y);
echo"\n Divisions:".($x/$y);
echo"\n Exponentation:".($x**$y);
echo"\n Modulus:".($x%$y);
?>

OUTPUT:
Arithmetic operations:
______________________:

Addition:14
Subtraction:6
Multiplications:40
Divisions:2.5
Exponentation:10000
Modulus:2

RESULT: Thus the program has been successfully executed.


PALINDROME

AIM:
To develop a PHP script that checks if a given string (word) is a palindrome. A
palindrome is a word that reads the same forward and backward (e.g., "madam").

ALGORITHM:
1) Start the program.
2) Define a function is Palindrome($string) to check if a given string is a
palindrome.
3) In the function:
o Use the built-in strrev() function to reverse the string.
o Compare the reversed string with the original string.
o If they are the same, return true (indicating the string is a
palindrome); otherwise, return false.
4) Declare a string variable $word (e.g., "madam").
5) Call the isPalindrome() function with the string $word as the argument.
6) Display a message indicating whether the string is a palindrome based on the
return value of the function.
7) End the program.
SOURCE CODE:
<?php
function isPalindrome($string)
{
if (strrev($string) == $string)
{
return true;
}
else
{
return false;
}
}
$word = "madam";
if (isPalindrome($word))
{
echo "$word is a palindrome.";
}
else
{
echo "$word is not a palindrome.";
}
?>

OUTPUT:
madam is a palindrome.

RESULT: Thus the program has been successfully executed.


LEAP YEAR

AIM:
To develop a PHP script that checks whether a given year is a leap year or
not.

ALGORITHM:
1) Start the program.
2) Define a function isLeapYear($year) that takes a year as an input.
3) Inside the function, check the following conditions:
 If the year is divisible by 400, it is a leap year.
 Else, if the year is divisible by 4 but not by 100, it is a leap year.
 Otherwise, it is not a leap year.
4) Print the result accordingly.
5) Declare a variable $year and assign a specific year (e.g., 1900).
6) Call the function isLeapYear($year) to check and display the result.
7) End the program.
SOURCE CODE:
<?php
function isLeapYear($year)
{
if (($year % 400 == 0) || ($year % 4 == 0 && $year % 100 != 0))
{
echo "$year is a leap year";
}
else
{
echo "$year is not a leap year";
}
}

$year = 1900;
isLeapYear($year);
?>

OUTPUT:
1900 is not a leap year

RESULT: Thus the program has been successfully executed.


CLASSIFIES THEIR AGE AS A MINOR, ADULT, OR
SENIOR CITIZEN

AIM:
To develop a PHP script that greets a user by name and classifies them based on
their age as a minor, adult, or senior citizen.

ALGORITHM:
1) Start the program.
2) Define a function greetUser($name, $age) that accepts two parameters:
o $name: The user's name.
o $age: The user's age.
3) Inside the function:
o Print a greeting message: "Hello, $name!".
o Use conditional statements to classify the user based on age:
 If age < 18: Print "You are a minor.".
 Else if age >= 18 and age < 60: Print "You are an adult.".
 Else: Print "You are a senior citizen.".
4) Declare variables $name and $age with appropriate values
(e.g., "John" and 25).
5) Call the function greetUser($name, $age).
6) End the program.
SOURCE CODE:
<?php
function greetUser($name, $age) {
echo "Hello, $name! ";
if ($age < 18) {
echo "You are a minor.\n";
}
elseif ($age >= 18 && $age < 60) {
echo "You are an adult.\n";
}
else
{
echo "You are a senior citizen.\n";
}
}
$name = "John";
$age = 25;
greetUser($name, $age);
?>

OUTPUT:
Hello, John! You are an adult.

RESULT: Thus the program has been successfully executed.


SWAPPING TWO NUMBERS

AIM:
To develop a PHP program to swapping two numbers using a temporary
variable.

ALGORITHM:
1) Start the program.
2) Declare two variables, a and b, and assign them values (a = 5, b = 10).
3) Print the values of a and b before swapping.
4) Use a temporary variable temp to store the value of a.
5) Assign the value of b to a.
6) Assign the value of temp (which holds the original value of a) to b.
7) Print the values of a and b after swapping.
8) End the program.
SOURCE CODE:
<?php
$a = 5;
$b = 10;
echo "Before swapping: a = $a, b = $b\n";
$temp = $a;
$a = $b;
$b = $temp;
echo "After swapping: a = $a, b = $b\n";
?>

OUTPUT:
Before swapping: a = 5, b = 10
After swapping: a = 10, b = 5

RESULT: Thus the program has been successfully executed.


AREA AND PERIMETER OF A RECTANGLE

AIM:
To develop a PHP program that calculates and displays the area and
perimeter of a rectangle given its length and width.

ALGORITHM:
1) Start the program.
2) Define a function and calculate Area($length, $width) that returns the
area using the formula: Area=length ×width\text{Area} = \text{length}
\times \text{width}Area= length ×width
3) Define a function calculate Perimeter($length, $width) that returns the
perimeter using the formula: Perimeter= 2×(length+width)\text{Perimeter}
= 2 \times (\text{length} + \text{width})Perimeter=2× (length+width)
4) Assign values to length and width (e.g., length = 10, width = 5).
5) Call the functions to compute the area and perimeter.
6) Display the length, width, area, and perimeter.
7) End the program.
SOURCE CODE:
<?php
function calculateArea($length, $width)
{
return $length * $width;
}
function calculatePerimeter($length, $width)
{
return 2 * ($length + $width);
}
$length = 10;
$width = 5;
$area = calculateArea($length, $width);
$perimeter = calculatePerimeter($length, $width);
echo "\n Length: $length <br>";
echo "\n Width: $width <br>";
echo "\n Area of Rectangle: $area <br>";
echo "\n Perimeter of Rectangle: $perimeter <br>";
?>

OUTPUT:
Length: 10 <br>
Width: 5 <br>
Area of Rectangle: 50 <br>
Perimeter of Rectangle: 30 <br>

RESULT: Thus the program has been successfully executed.


COMPARE TWO STRINGS

AIM:
To write a PHP program that compares two strings and determines whether
they are equal or not.

ALGORITHM:
1) Start the program.
2) Accept two strings as input (either hardcoded or from the user).
3) Use a comparison method:
 Case-sensitive comparison: Use the === operator.
 Case-insensitive comparison: Use strcasecmp().
4) If the strings are equal, print "The strings are equal."
5) Otherwise, print "The strings are not equal."
6) Display the original strings and the result.
7) End the program.
SOURCE CODE:
<?php
function compareStrings($str1, $str2)
{
if ($str1 === $str2)
{
return "The strings are equal.";
}
else
{
return "The strings are not equal.";
}
}
$string1 = "Hello";
$string2 = "hello";
$result = compareStrings($string1, $string2);
echo "\n String 1: $string1";
echo "\n String 2: $string2";
echo "\n Comparison Result: $result";
?>

OUTPUT:
String 1: Hello
String 2: hello
Comparison Result: The strings are not equal.

RESULT: Thus the program has been successfully executed.

You might also like