Arithmetic, comparison, logical operators php

Arithmetic, comparison, logical operators php

In PHP web development, arithmetic operators, comparison operators, and logical operators are used to perform various operations on program variables and data type values. These operators can be used in multiple categories within PHP development.

Arithmetic, comparison, logical operators php

Let’s learn more about arithmetic, comparison, and logical operators in PHP web development.

Arithmetic Operators in PHP Development.

In PHP programming, arithmetic operators are used to perform numerical calculations, including mathematical operations such as addition, subtraction, multiplication, division, and modulus.

Operator nameArithmetic operator DescriptionExample
+ Plus, operatorUsed to Addition two variable value$p + $q
– Minus, operatorUsed to Subtraction two variable value$p – $q
* Multiply, operatorUsed to Multiplication two variable value$p * $q
/ Division, operatorUsed to Division two variable value$p / $q
% Modulus, operatorUsed to Modulus (remainder of division)$p % $q
++ Increment, operatorIt Increment variable value increase by 1++$p, $p++
— Decrement, operatorIt Decrement variable value decrease by 1–$p, $p–

Example of Arithmetic Operators.

<?php

$p = 7;

$q = 3;

echo $p + $q; // Result – 10

echo “\n”;

echo $p – $q; // Result – 4

echo “\n”;

echo $p * $q; // Result – 21

echo “\n”;

echo $p / $q; // Result – 2.3

echo “\n”;

echo $p % $q; // Result – 1 (remainder of division)

echo “\n”;

// Increment/Decrement operators

echo ++$p; // Result – 8 (increments before output)

echo “\n”;

echo $q–; // Result – 3 (outputs then decrements)

?>

Arithmetic Operators Explanation.

  • Pre-increment (++$p) – Here, the ++ pre-increment operator increases the value of the $p variable before processing.
  • Post-increment ($p++) – Here, it uses the value of the $p variable first, then increments it in the post-increment operation.
  • Similarly, pre-decrement (–$p) and post-decrement ($p–) variable operations can be applied in PHP programming as needed.

Comparison Operators in PHP Development.

In PHP programming, comparison operators are used to compare two different program variable values. Here, if the comparison between the two variable values ​​is successful, it returns a true value as the result, and if the comparison fails, it returns false.

Operator nameComparison Operators DescriptionExample
== Equal, operatorIt used to check Equal to program variable value$p == $q
!= or <> Not Equal, operatorIt used to check Not equal to variable condition$p != $q or $p <> $q
=== Identical, operatorIt used to check Identical (equal and same type) program condition$p === $q
!== Not Identical, operatorIt used to check Not identical (not equal or different type) variable value$p !== $q
> Greater than, operatorIt used to check Greater than program variable value$p > $q
< Less than, operatorIt used to check Less than program variable value$p < $q
>= Greater than or equal to, operatorIt used to check Greater than or equal to program value$p >= $q
<=  Less than or equal to, operatorIt used to check Less than or equal to program variable value$p <= $q

Example of Comparison Operators.

<?php

$p = 3;

$q = 4;

$r = “8”;

var_dump($p == $q); // Result – bool(false)

var_dumpy($p != $q); // Result – bool(true)

var_dump($p === $r); // Result – bool(false)

var_dump($p > $q); // Result – bool(false)

var_dump($p <= $q); // Result – bool(true)

?>

Comparison Operators Explanation.

  • The equality operator (==) checks if the values ​​of two variables are equal, regardless of their data type.
  • The identity operator (===) checks if the values ​​of two variables are equal and if they are of the same data type.
  • The not equal to operators (!= and <>) are both used to perform a “not equal to” comparison.

Logical Operators in PHP Development.

Logical operators in PHP are used to evaluate conditional expressions and return a boolean value (true or false).

Operator NameLogical Operators DescriptionExample
&& And, operatorIt used to check combined And returns true if both conditions are true at the same time$p && $q
! Not, operatorIt used to test Not inverts the program boolean variable value!$p
And and, operatorIt used to check And similar to &&, lower precedence program variable value condition$p and $q
Or or, operatorIt used to check Or similar to or operator condition 
Xor Exclusive Or, operatorIt used to check Exclusive Or true if only one condition is true in given program expression$p xor $q

Example of Logical Operators.

<?php

$p = true;

$q = false;

var_dump($p && $q); // Result – bool(false) (The AND operator checks if both conditions are true)

var_dump($p || $q); // Result – bool(true) (The OR operator checks if at least one condition is true)

var_dmp(!$p); // Result – bool(false) (The NOT operator inverts the value of the $p variable)

var_dump($p xor $q); // Result – bool(true) (The XOR operator returns true if only one condition is true, not both)

?>

Logical Operators Explanation.

  • && and AND operator – Here, the AND operator checks both variables to see if both conditions are true at the same time.  The && operator has higher precedence than the word “and”.
  • || and or operator – Here, the OR operator checks if at least one of the conditions is true. The || operator has higher precedence than the word “or”.
  • xor operator – The XOR operator returns true if exactly one of the conditions is true; it is also known as the exclusive OR operator.

Combining Multiple Operators in PHP Programming.

In PHP programming, you can combine arithmetic, comparison, and logical operators to create complex expressions.

  • Arithmetic Operator Explanation.
  • Comparison Operator Explanation.
  • Logical Operator Explanation.

Example of combined operators in PHP programming.

<?php

$p = 3;

$q = 7;

$r = 5;

// Here, the condition 3 + 7 > 5 is true, because 10 > 5 is true

$output = ($p + $q) > $r && $p != $r;

var_dump($output); // Result – bool(true)

?>

Operator Precedence. Here, parentheses (()) have higher precedence, and they can be used to indicate the evaluation sequence in a clear order. Arithmetic operators *, /, and % are evaluated before comparison operators (==, !=, >), and these in turn have higher precedence than logical operators (&&, ||).

Summary of arithmetic, comparison, and logical operators in PHP development.

  • Arithmetic Operators – In PHP programming, the operators +, -, *, /, %, ++, and — are used to perform arithmetic mathematical operations, e.g., $p + $q, $p * $q.
  • Comparison Operators – In PHP programming, the comparison operators ==, !=, ===, !==, >, <, >=, and <= are used to compare multiple program values, e.g., $a == $b, $a < $b.
  • Logical Operators – The logical operators && and || are used to evaluate AND and OR conditions.

Leave a Reply