PHP Operators - Codingque

PHP Operators

Understand the different operators in PHP with examples and explanations.

Introduction to PHP Operators

Operators in PHP are special symbols or keywords used to perform operations on operands. PHP supports several types of operators including arithmetic, comparison, logical, and assignment operators.

1. Arithmetic Operators

Arithmetic operators are used to perform basic arithmetic operations:

<?php
$a = 10;
$b = 5;

echo "Addition: " . ($a + $b) . "<br>"; // Outputs: Addition: 15
echo "Subtraction: " . ($a - $b) . "<br>"; // Outputs: Subtraction: 5
echo "Multiplication: " . ($a * $b) . "<br>"; // Outputs: Multiplication: 50
echo "Division: " . ($a / $b) . "<br>"; // Outputs: Division: 2
echo "Modulus: " . ($a % $b) . "<br>"; // Outputs: Modulus: 0
?>

Explanation: The arithmetic operators perform basic mathematical operations. The + operator adds, - subtracts, * multiplies, / divides, and % finds the remainder.

2. Comparison Operators

Comparison operators are used to compare two values and return a boolean result:

<?php
$a = 10;
$b = 5;

echo "Equal: " . ($a == $b ? "True" : "False") . "<br>"; // Outputs: Equal: False
echo "Not Equal: " . ($a != $b ? "True" : "False") . "<br>"; // Outputs: Not Equal: True
echo "Greater Than: " . ($a > $b ? "True" : "False") . "<br>"; // Outputs: Greater Than: True
echo "Less Than: " . ($a < $b ? "True" : "False") . "<br>"; // Outputs: Less Than: False
echo "Greater or Equal: " . ($a >= $b ? "True" : "False") . "<br>"; // Outputs: Greater or Equal: True
echo "Less or Equal: " . ($a <= $b ? "True" : "False") . "<br>"; // Outputs: Less or Equal: False
?>

Explanation: Comparison operators compare two values. The == operator checks for equality, != checks for inequality, > checks if one value is greater, < checks if it is smaller, >= checks if it is greater or equal, and <= checks if it is less or equal.

3. Logical Operators

Logical operators are used to combine conditional statements:

<?php
$a = true;
$b = false;

echo "AND: " . ($a && $b ? "True" : "False") . "<br>"; // Outputs: AND: False
echo "OR: " . ($a || $b ? "True" : "False") . "<br>"; // Outputs: OR: True
echo "XOR: " . ($a xor $b ? "True" : "False") . "<br>"; // Outputs: XOR: True
echo "NOT: " . (!$a ? "True" : "False") . "<br>"; // Outputs: NOT: False
?>

Explanation: Logical operators are used to combine or invert boolean values. The && operator returns true if both operands are true, || returns true if at least one operand is true, xor returns true if exactly one operand is true, and ! inverts the boolean value.

4. Assignment Operators

Assignment operators are used to assign values to variables with optional operations:

<?php
$a = 10;
$a += 5; // Equivalent to $a = $a + 5
echo "Addition Assignment: " . $a . "<br>"; // Outputs: Addition Assignment: 15

$a -= 3; // Equivalent to $a = $a - 3
echo "Subtraction Assignment: " . $a . "<br>"; // Outputs: Subtraction Assignment: 12

$a *= 2; // Equivalent to $a = $a * 2
echo "Multiplication Assignment: " . $a . "<br>"; // Outputs: Multiplication Assignment: 24

$a /= 4; // Equivalent to $a = $a / 4
echo "Division Assignment: " . $a . "<br>"; // Outputs: Division Assignment: 6

$a %= 3; // Equivalent to $a = $a % 3
echo "Modulus Assignment: " . $a . "<br>"; // Outputs: Modulus Assignment: 0
?>

Explanation: Assignment operators assign values to variables and perform an operation in the process. The +=, -=, *=, /=, and %= operators perform addition, subtraction, multiplication, division, and modulus respectively, while assigning the result back to the variable.

Previous Next
Modern Footer