Introduction:
Explore the fundamental arithmetic operations in PHP with this comprehensive lesson. Learn how to perform addition, subtraction, multiplication, division, and more. Boost your PHP skills and gain a solid understanding of mathematical functions for web development.
In PHP, there are several built-in functions that you can use for basic arithmetic operations. Here are some of the commonly used ones:
$sum = 2 + 3; // $sum will be 5
$difference = 5 – 2; // $difference will be 3
$product = 4 * 6; // $product will be 24
$quotient = 8 / 2; // $quotient will be 4
$remainder = 7 % 3; // $remainder will be 1
$power = 2 ** 3; // $power will be 8
// Alternatively:
$power_alternative = pow(2, 3); // $power_alternative will also be 8
These are basic arithmetic operations. Additionally, PHP provides functions for more advanced mathematical operations in the math extension, such as sqrt() for square root, abs() for absolute value, and more.
Here’s a simple example using some of these functions:
$a = 10; $b = 3; $sum = $a + $b; $difference = $a - $b; $product = $a * $b; $quotient = $a / $b; $remainder = $a % $b; $power = $a ** $b; echo "Sum: $sum<br>"; echo "Difference: $difference<br>"; echo "Product: $product<br>"; echo "Quotient: $quotient<br>"; echo "Remainder: $remainder<br>"; echo "Power: $power<br>";
This will output:
Sum: 13
Difference: 7
Product: 30
Quotient: 3.3333333333333
Remainder: 1
Power: 1000
PHP provides various built-in functions for basic arithmetic operations. Here are some of the commonly used ones:
$number = -5;
$absoluteValue = abs($number); // $absoluteValue will be 5
$number = 9;
$squareRoot = sqrt($number); // $squareRoot will be 3
$floatNumber = 3.14159;
$roundedNumber = round($floatNumber); // $roundedNumber will be 3
$floatNumber = 4.2;
$ceiledNumber = ceil($floatNumber); // $ceiledNumber will be 5
$floatNumber = 4.9;
$flooredNumber = floor($floatNumber); // $flooredNumber will be 4
$minValue = min(5, 8, 3); // $minValue will be 3
$maxValue = max(5, 8, 3); // $maxValue will be 8
$randomNumber = rand(1, 10); // Generates a random number between 1 and 10
These functions can be used to perform basic arithmetic operations and manipulate numeric values in PHP. Keep in mind that some functions may take multiple parameters or have additional optional parameters, so it’s always a good idea to check the official PHP documentation for more details.
complete example about each type in html page with explanation
Below is an HTML page with examples for each type of basic arithmetic operation using PHP. Each example is accompanied by an explanation:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>PHP Arithmetic Examples</title> </head> <body> <h1>PHP Arithmetic Examples</h1> <?php // Addition $sum = 2 + 3; echo "<p>Addition: 2 + 3 = $sum</p>"; // Subtraction $difference = 5 - 2; echo "<p>Subtraction: 5 - 2 = $difference</p>"; // Multiplication $product = 4 * 6; echo "<p>Multiplication: 4 * 6 = $product</p>"; // Division $quotient = 8 / 2; echo "<p>Division: 8 / 2 = $quotient</p>"; // Modulus $remainder = 7 % 3; echo "<p>Modulus: 7 % 3 = $remainder</p>"; // Exponentiation $power = 2 ** 3; echo "<p>Exponentiation: 2 ** 3 = $power</p>"; // Absolute Value $number = -5; $absoluteValue = abs($number); echo "<p>Absolute Value of -5: $absoluteValue</p>"; // Square Root $number = 9; $squareRoot = sqrt($number); echo "<p>Square Root of 9: $squareRoot</p>"; // Round $floatNumber = 3.14159; $roundedNumber = round($floatNumber); echo "<p>Rounded Number: $roundedNumber</p>"; // Ceiling $floatNumber = 4.2; $ceiledNumber = ceil($floatNumber); echo "<p>Ceiled Number: $ceiledNumber</p>"; // Floor $floatNumber = 4.9; $flooredNumber = floor($floatNumber); echo "<p>Floored Number: $flooredNumber</p>"; // Minimum $minValue = min(5, 8, 3); echo "<p>Minimum Value: $minValue</p>"; // Maximum $maxValue = max(5, 8, 3); echo "<p>Maximum Value: $maxValue</p>"; // Random Number $randomNumber = rand(1, 10); echo "<p>Random Number between 1 and 10: $randomNumber</p>"; ?> </body> </html>
Explanation:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>PHP Arithmetic Calculator</title> </head> <body> <h1>PHP Arithmetic Calculator</h1> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <label for="num1">Enter the first number:</label> <input type="number" name="num1" required> <label for="num2">Enter the second number:</label> <input type="number" name="num2" required> <button type="submit">Calculate</button> </form> <?php // Check if the form is submitted if ($_SERVER["REQUEST_METHOD"] == "POST") { // Retrieve user input $num1 = $_POST["num1"]; $num2 = $_POST["num2"]; // Perform arithmetic operations $sum = $num1 + $num2; $difference = $num1 - $num2; $product = $num1 * $num2; $quotient = ($num2 != 0) ? $num1 / $num2 : "Cannot divide by zero"; $remainder = ($num2 != 0) ? $num1 % $num2 : "Cannot divide by zero"; // Display the results echo "<h2>Results:</h2>"; echo "<p>$num1 + $num2 = $sum</p>"; echo "<p>$num1 - $num2 = $difference</p>"; echo "<p>$num1 * $num2 = $product</p>"; echo "<p>$num1 / $num2 = $quotient</p>"; echo "<p>$num1 % $num2 = $remainder</p>"; } ?> </body> </html>
Explanation:
Here’s a set of 15 quiz questions related to the basic arithmetic operations in PHP. Each question is followed by multiple-choice answers. Feel free to use or modify this quiz for your needs.
Quiz: Basic Arithmetic Operations in PHP
a) 10
b) 73
c) 21
d) 4
a) sqrt()
b) squareRoot()
c) root()
d) power()
a) round()
b) ceil()
c) floor()
d) abs()
a) 2
b) 2.5
c) 1
d) 0
a) abs()
b) absolute()
c) positive()
d) value()
a) 8
b) 4
c) 2
d) 14
a) 1 to 10
b) 1 to 100
c) 0 to 100
d) 10 to 1000
a) power()
b) exp()
c) pow()
d) ^
a) minimum()
b) min()
c) smallest()
d) smaller()
a) 0
b) 6
c) Error (division by zero)
d) 1
a) max()
b) maximum()
c) largest()
d) biggest()
a) round()
b) ceil()
c) floor()
d) abs()
a) 81
b) 12
c) 7
d) 64
a) 20
b) 4
c) 5
d) 10
a) mod()
b) remainder()
c) modulus()
d) %
Answers:
1-a) 10
2-a) sqrt()
3-a) round()
4-c) 1
5-a) abs()
6-a) 8
7-b) 1 to 100
8-c) pow()
9-b) min()
10-c) Error (division by zero)
11-a) max()
12-c) floor()
13-a) 81
14-c) 5
15-d) %