Problem: Write a php script to demonstrate arithmetic operators, comparison operator, and logical operator.
Solution: PHP script that demonstrates the use of arithmetic operators, comparison operators, and logical operators:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | <?php // Arithmetic Operators $a = 10; $b = 20; // Addition $result = $a + $b; echo "Addition: $a + $b = $result\n"; // Subtraction $result = $b - $a; echo "Subtraction: $b - $a = $result\n"; // Multiplication $result = $a * $b; echo "Multiplication: $a * $b = $result\n"; // Division $result = $b / $a; echo "Division: $b / $a = $result\n"; // Modulus $result = $b % $a; echo "Modulus: $b % $a = $result\n"; echo "\n"; // Comparison Operators $c = 15; // Less Than $result = ($a < $b); echo "Less Than: $a < $b is " . ($result ? 'true' : 'false') . "\n"; // Greater Than $result = ($a > $b); echo "Greater Than: $a > $b is " . ($result ? 'true' : 'false') . "\n"; // Less Than or Equal To $result = ($a <= $b); echo "Less Than or Equal To: $a <= $b is " . ($result ? 'true' : 'false') . "\n"; // Greater Than or Equal To $result = ($a >= $b); echo "Greater Than or Equal To: $a >= $b is " . ($result ? 'true' : 'false') . "\n"; // Equal To $result = ($a == $c); echo "Equal To: $a == $c is " . ($result ? 'true' : 'false') . "\n"; // Not Equal To $result = ($a != $c); echo "Not Equal To: $a != $c is " . ($result ? 'true' : 'false') . "\n"; echo "\n"; // Logical Operators $d = true; $e = false; // And $result = ($d and $e); echo "And: $d and $e is " . ($result ? 'true' : 'false') . "\n"; // Or $result = ($d or $e); echo "Or: $d or $e is " . ($result ? 'true' : 'false') . "\n"; // Not $result = !$d; echo "Not: !$d is " . ($result ? 'true' : 'false') . "\n"; ?> |
When run, this script outputs:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | Addition: 10 + 20 = 30 Subtraction: 20 - 10 = 10 Multiplication: 10 * 20 = 200 Division: 20 / 10 = 2 Modulus: 20 % 10 = 0 Less Than: 10 < 20 is true Greater Than: 10 > 20 is false Less Than or Equal To: 10 <= 20 is true Greater Than or Equal To: 10 >= 20 is false Equal To: 10 == 15 is false Not Equal To: 10 != 15 is true And: true and false is false Or: true or false is true Not: !true is false |
In this script, we perform various arithmetic operations, such as addition, subtraction, multiplication, division, and modulus. We also use comparison operators, such as <
, >
, <=
, >=
, ==
, and !=
, to compare values and produce boolean results. Finally, we use logical operators, such as and
, or
, and !
, to perform operations on boolean values and produce a boolean result.
Let me explain what’s happening in this script:
Arithmetic Operators: In this section, we perform basic arithmetic operations, such as addition, subtraction, multiplication, division, and modulus, using the +
, -
, *
, /
, and %
operators, respectively. We assign the values of 10
to $a
and 20
to $b
, and then perform the arithmetic operations on these variables and assign the result to the $result
variable. Finally, we use echo
to output the result of each operation.
Comparison Operators: In this section, we compare the values of $a
and $b
using various comparison operators, such as <
, >
, <=
, >=
, ==
, and !=
. These operators produce a boolean result indicating whether the comparison is true or false. We also compare the value of $a
with $c
, which is assigned the value of 15
. Finally, we use the ternary operator ($result ? 'true' : 'false')
to output either true
or false
based on the result of each comparison.
Logical Operators: In this section, we perform operations on boolean values using the logical operators and
, or
, and !
. The and
operator returns true
only if both operands are true
. The or
operator returns true
if either or both operands are true
. The !
operator negates a boolean value, so !true
is false
and !false
is true
. We assign the values of true
to $d
and false
to $e
, and then perform the logical operations on these variables and assign the result to the $result
variable. Finally, we use echo
to output the result of each operation.