0% found this document useful (0 votes)
18 views6 pages

Experiment No. - 1: Date of Performance: Date of Submission

Uploaded by

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

Experiment No. - 1: Date of Performance: Date of Submission

Uploaded by

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

Experiment No.

- 1

Date of Performance: 07-01-2025

Date of Submission: 21-01-2025

Program Execution/ Timely Viva Experiment Sign with Date


formation/ Submissi (03) Total (10)
correction/ on (01)
ethical practices
(06)

Experiment No. - 1
PHP script to perform simple arithmetic operations and display the result.

1.1 Aim:
To develop a PHP script that performs basic arithmetic operations (addition, subtraction,
multiplication, and division) and displays the results dynamically.

1.2 Course Outcome:


Understand and apply fundamental programming concepts in PHP to execute arithmetic
operations and generate output dynamically.

1.3 Learning Objective:


● To learn the syntax and basic operations in PHP.

● To implement user input handling and perform arithmetic calculations.

● To display the results of arithmetic operations in a structured manner.

1.4 Related Theory:


PHP (Hypertext Preprocessor) is a widely used scripting language designed for web
development. It supports various built-in operators to perform mathematical computations.
Arithmetic operations such as addition, subtraction, multiplication, division, and modulus are
fundamental in any programming language, including PHP.

A PHP script that performs arithmetic operations follows a structured approach that involves
input handling, processing, and output generation.

PHP provides several arithmetic operators that allow mathematical calculations. These operators
work with numeric values (integers and floating points) and return computed results.

Operator Symbol Description Example

Addition + Adds two value 5+3=8

Subtraction - Subtracts one value from 10 - 1 = 9


another

Multiplication * Multiplies two value 5 * 3 = 15

Division / Divides one value from 20 / 5 = 4


another

Modulus % Returns remainder after 10 % 3 = 1


division

Key Concepts of Arithmetic Operations in PHP

1. Variables in PHP

● Variables are used to store numeric values for calculations.

● In PHP, variables are declared using the $ symbol followed by the variable
name.

● PHP is loosely typed, meaning variables do not require explicit data type
declarations.

2. Arithmetic Operators in PHP

PHP supports various arithmetic operators for performing calculations:

● Addition (+): Adds two or more numbers.

● Subtraction (-): Subtracts one number from another.

● Multiplication (*): Multiplies two numbers.

● Division (/): Divides one number by another.

● Modulus (%): Returns the remainder of a division operation.

3. Order of Operations (Operator Precedence)

PHP follows the standard mathematical order of operations:

a. Parentheses ()

b. Exponentiation **

c. Multiplication *, Division /, and Modulus %

d. Addition + and Subtraction -


Operations enclosed within parentheses are executed first.

4. Handling Arithmetic Operations in PHP

● Arithmetic operations in PHP are performed using standard mathematical


symbols.

● The calculated results can be stored in variables.

● PHP can handle calculations with integers (whole numbers) and floating-point
numbers (decimals).

5. Displaying Results in PHP

● PHP uses the echo or print statement to output the results.

● Results can be displayed directly on a webpage using HTML formatting.

● PHP can dynamically update results when input values change.

Practical Applications of PHP Arithmetic Operations

1. Online Calculators – Used in financial, scientific, and statistical computations.

2. E-commerce Websites – Price calculations, discounts, tax computations, and total


billing.

3. Data Analytics – Processing numerical data from databases.

4. Banking & Finance – Interest calculations, loan computations, and investment


projections.

5. Gaming Applications – Score calculations, damage calculations, and physics-based


simulations.

Error Handling in Arithmetic Operations

● Division by Zero: PHP generates a warning when dividing by zero, which can
be handled using conditional statements.
● Floating-Point Precision Issues: Floating-point calculations may result in
precision errors, which can be controlled using functions like round(), floor(),
or ceil().

● Data Type Mismatch: Performing operations on non-numeric values can lead


to unexpected results or errors.

1.5 PHP Code Implementation:


<!DOCTYPE html>
<html>
<head>
<title>Basic Arithmetic Operations.</title>
</head>
<body>

<?php
$num1 = 10;
$num2 = 5;

$result = $num1 + $num2;


echo "The Addition is: $result<br>";

$result = $num1 - $num2;


echo "The Subtraction is: $result<br>";

$result = $num1 * $num2;


echo "The Multiplication is: $result<br>";

if ($num2 != 0) {
$result = $num1 / $num2;
echo "The Division is: $result";
} else {
echo "Division by zero is not allowed.";
}

?>

</body>
</html>
1.6 Execution Steps:
1. Install a local server like XAMPP or WAMP.

2. Create a PHP file (arithmetic.php) and paste the above code.

3. Place the file inside the htdocs folder (for XAMPP) or the www folder (for WAMP).

4. Start the Apache server from the XAMPP/WAMP control panel.

5. Open a web browser and visit http://localhost/arithmetic.php.

1.7 Output :

1.8 Conclusion :
In conclusion, I have successfully written a PHP code that performs basic arithmetic operations,
such as addition, subtraction, multiplication, and division. This program provides a simple yet
effective way to handle mathematical calculations and demonstrates the practical use of PHP for
solving everyday problems. The code is designed to be efficient, easy to understand, and can be
expanded upon for more complex operations in the future.

You might also like