Introduction:
Welcome to our comprehensive guide on PHP arithmetic operators! In this lesson, we’ll explore the fundamental arithmetic operators in PHP, covering addition, subtraction, multiplication, division, modulus, and more. You’ll also learn about increment and decrement operations. Follow along with practical examples and a quiz to reinforce your understanding.
In PHP, arithmetic operators are used to perform various mathematical operations on numeric values. Here are the basic arithmetic operators in PHP:
Adds two values together.
$sum = $a + $b;
Subtracts the right operand from the left operand.
$difference = $a - $b;
Multiplies two values.
$product = $a * $b;
Divides the left operand by the right operand.
$quotient = $a / $b;
Returns the remainder of the division of the left operand by the right operand.
$remainder = $a % $b;
Raises the left operand to the power of the right operand.
$result = $a ** $b;
Increment increases the value of a variable by 1.
Decrement decreases the value of a variable by 1.
$a++; $b--;
Here’s an example that combines these operators:
$a = 10; $b = 5; $sum = $a + $b; $difference = $a - $b; $product = $a * $b; $quotient = $a / $b; $remainder = $a % $b; $result = $a ** $b; echo "Sum: $sum<br>"; echo "Difference: $difference<br>"; echo "Product: $product<br>"; echo "Quotient: $quotient<br>"; echo "Remainder: $remainder<br>"; echo "Result: $result<br>"; // Increment and Decrement $a++; $b--; echo "Incremented a: $a<br>"; echo "Decremented b: $b<br>";
This script initializes variables $a and $b with values, performs various arithmetic operations, and prints the results. The increment and decrement operations are also demonstrated at the end.
complete example in html with explanation
Let’s create a simple HTML file that includes a PHP script with arithmetic operators. I’ll provide comments to explain each part of the code.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>PHP Arithmetic Operators Example</title> </head> <body> <h1>PHP Arithmetic Operators Example</h1> <?php // PHP code starts here // Initializing variables $a = 10; $b = 5; // Arithmetic operations $sum = $a + $b; $difference = $a - $b; $product = $a * $b; $quotient = $a / $b; $remainder = $a % $b; $result = $a ** $b; // Increment and Decrement $a++; $b--; // Output the results echo "<p>Value of a: $a</p>"; echo "<p>Value of b: $b</p>"; echo "<p>Sum: $sum</p>"; echo "<p>Difference: $difference</p>"; echo "<p>Product: $product</p>"; echo "<p>Quotient: $quotient</p>"; echo "<p>Remainder: $remainder</p>"; echo "<p>Result: $result</p>"; ?> </body> </html>
Explanation:
Save this code in a file with a .php extension (e.g., arithmetic_example.php).
When you open this file in a web browser, you should see the results of the arithmetic operations displayed on the webpage.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Arithmetic Operations App</title> <style> input { width: 50px; } </style> </head> <body> <h1>Arithmetic Operations App</h1> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <label for="num1">Enter Number 1:</label> <input type="text" name="num1" id="num1" required> <label for="num2">Enter Number 2:</label> <input type="text" name="num2" id="num2" required> <button type="submit">Calculate</button> </form> <?php // PHP code starts here // Check if the form is submitted if ($_SERVER['REQUEST_METHOD'] === 'POST') { // Retrieve input $num1 = $_POST['num1']; $num2 = $_POST['num2']; // Validate input (check if they are numeric) if (is_numeric($num1) && is_numeric($num2)) { // Perform arithmetic operations $sum = $num1 + $num2; $difference = $num1 - $num2; $product = $num1 * $num2; $quotient = $num1 / $num2; $remainder = $num1 % $num2; $result = $num1 ** $num2; // Output the results echo "<h2>Results:</h2>"; echo "<p>Sum: $sum</p>"; echo "<p>Difference: $difference</p>"; echo "<p>Product: $product</p>"; echo "<p>Quotient: $quotient</p>"; echo "<p>Remainder: $remainder</p>"; echo "<p>Result: $result</p>"; } else { // Display an error message if input is not numeric echo "<p style='color: red;'>Please enter valid numeric values.</p>"; } } ?> </body> </html>
Explanation:
Here’s a quiz with 10 questions about PHP arithmetic operators.
PHP Arithmetic Operators Quiz
a) 10
b) 5
c) 15
d) 50
a) +
b) *
c) /
d) –
a) Multiplication
b) Division
c) Returns the remainder of a division
d) Exponentiation
a) ++
b) —
c) +=
d) -=
a) Modulus
b) Exponentiation
c) Multiplication
d) Division
a) /
b) *
c) %
d) **
a) 9
b) 8
c) 10
d) 7
a) .
b) +
c) &
d) ,
a) 6.66
b) 7
c) 6
d) 6.33
a) —
b) +=
c) -=
d) ++
Answers:
1-c) 15
2-d) –
3-c) Returns the remainder of a division
4-a) ++
5-b) Exponentiation
6-b) *
7-a) 9
8-a) .
9-a) 6.66
10-a) —