PHP

PHP Script to Demonstrate Arithmetic Operators, Comparison Operator, and Logical operator3 min read

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:




When run, this script outputs:

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.

Leave a Comment