PHP Operators

PHP operators are symbols used to perform different types of operations on variables, values, and expressions. Here are the different types of PHP operators:

  1. Arithmetic operators
  2. Assignment operators
  3. Comparison operators
  4. Increment/Decrement operators
  5. Logical operators
  6. String operators
  7. Array operators
  8. Conditional assignment operators

1. PHP Arithmetic Operators

Arithmetic operators are used to perform mathematical operations. The arithmetic operators are:

OperatorNameExampleResult
+Addition$x + $ySum of $x & $y
Substraction$x – $yDifference of $x & $y
*Multiplication$x * $yProduct of $x & $y
/Division$x / $yQuotient of $x & $y
%Modulus$x % $yRemainder of $x divided by $y

Here are some examples of arithmetic operators in PHP:

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

// Addition
$c = $a + $b;
echo "Addition: " . $c . "<br>";

// Subtraction
$c = $a - $b;
echo "Subtraction: " . $c . "<br>";

// Multiplication
$c = $a * $b;
echo "Multiplication: " . $c . "<br>";

// Division
$c = $a / $b;
echo "Division: " . $c . "<br>";

// Modulus
$c = $a % $b;
echo "Modulus: " . $c . "<br>";

// Exponentiation
$c = $a ** $b;
echo "Exponentiation: " . $c . "<br>";
?>

Output:

Addition: 15
Subtraction: 5
Multiplication: 50
Division: 2
Modulus: 0
Exponentiation: 100000

In the above code, we have assigned values of 10 and 5 to variables $a and $b, respectively. Then, we have used arithmetic operators such as addition, subtraction, multiplication, division, modulus, and exponentiation to perform different types of operations on these variables. Finally, we have printed the result of each operation using the echo statement.

2. PHP Assignment Operators

Assignment operators are used to assign values to variables. The assignment operators are:

  • Assignment (=)
  • Addition Assignment (+=)
  • Subtraction Assignment (-=)
  • Multiplication Assignment (*=)
  • Division Assignment (/=)
  • Modulus Assignment (%=)

Here are some examples of assignment operators in PHP:

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

// Addition assignment
$a += $b;
echo "Addition assignment: " . $a . "<br>";

// Subtraction assignment
$a -= $b;
echo "Subtraction assignment: " . $a . "<br>";

// Multiplication assignment
$a *= $b;
echo "Multiplication assignment: " . $a . "<br>";

// Division assignment
$a /= $b;
echo "Division assignment: " . $a . "<br>";

// Modulus assignment
$a %= $b;
echo "Modulus assignment: " . $a . "<br>";
?>

Output:

Addition assignment: 15
Subtraction assignment: 10
Multiplication assignment: 50
Division assignment: 10
Modulus assignment: 0

In the above code, we have assigned values of 10 and 5 to variables $a and $b, respectively. Then, we have used assignment operators such as addition assignment, subtraction assignment, multiplication assignment, division assignment, and modulus assignment to perform different types of operations on these variables. Finally, we have printed the result of each operation using the echo statement.

3. PHP Comparison Operators

Comparison operators are used to compare two values. The comparison operators are:

OperatorsNameExampleDescription
==Equal to$x==$yReturns true if $x is equal to $y
===Identical to$x===$yReturns true if $x is equal and same type of $y.
!=Not equal to$x != $yReturns true if $x is not equal to $y.
<>Not equal to$x <> $yReturns true if $x is not equal to $y.
!==Not identical to$x !== $yReturns true if $x is not equal or are not the same types to $y
>Greater than$x>$yReturns true if the $x is greater than $y
<Less than$x<$yReturns true if the $x is less than $y
>=Greater than or equal to $x >= $yReturns true if the $x is greater than or equal to $y
<=Less than or equal to $x <= $yReturns true if the $x is less than or equal to $y

Here are some examples of comparison operators in PHP:

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

// Equal to
if ($a == $c) {
    echo "Equal to: true<br>";
} else {
    echo "Equal to: false<br>";
}

// Identical to
if ($a === $c) {
    echo "Identical to: true<br>";
} else {
    echo "Identical to: false<br>";
}

// Not equal to
if ($a != $b) {
    echo "Not equal to: true<br>";
} else {
    echo "Not equal to: false<br>";
}

// Not identical to
if ($a !== $c) {
    echo "Not identical to: true<br>";
} else {
    echo "Not identical to: false<br>";
}

// Greater than
if ($a > $b) {
    echo "Greater than: true<br>";
} else {
    echo "Greater than: false<br>";
}

// Less than
if ($a < $b) {
    echo "Less than: true<br>";
} else {
    echo "Less than: false<br>";
}

// Greater than or equal to
if ($a >= $c) {
    echo "Greater than or equal to: true<br>";
} else {
    echo "Greater than or equal to: false<br>";
}

// Less than or equal to
if ($a <= $c) {
    echo "Less than or equal to: true<br>";
} else {
    echo "Less than or equal to: false<br>";
}
?>

Output:

Equal to: true
Identical to: false
Not equal to: true
Not identical to: true
Greater than: true
Less than: false
Greater than or equal to: true
Less than or equal to: true

In the above code, we have assigned values of 10 and 5 to variables $a and $b, respectively. We have also assigned the string value of “10” to variable $c. Then, we have used comparison operators such as equal to, identical to, not equal to, not identical to, greater than, less than, greater than or equal to, and less than or equal to to compare these variables. Finally, we have printed the result of each comparison using the echo statement.

4. PHP Increment / Decrement Operators

Increment and decrement operators are used to increase or decrease the value of a variable. The increment and decrement operators are:

OperatorsNameDescription
++$xPre-incrementIncrements $x by one, and returns $x.
$x++Post-incrementReturns $x first, then increment $x by one.
– -$xPre-decrementDecrements $x by one and returns $x.
$x- –Post-decrementReturns $x first then decrements $x by one.

Here are some examples of increment and decrement operators in PHP:

<?php
$a = 5;

// Post-increment
echo "Post-increment: " . $a++ . "<br>";
echo "Value of a after post-increment: " . $a . "<br>";

// Pre-increment
echo "Pre-increment: " . ++$a . "<br>";
echo "Value of a after pre-increment: " . $a . "<br>";

// Post-decrement
echo "Post-decrement: " . $a-- . "<br>";
echo "Value of a after post-decrement: " . $a . "<br>";

// Pre-decrement
echo "Pre-decrement: " . --$a . "<br>";
echo "Value of a after pre-decrement: " . $a . "<br>";
?>

Output:

Post-increment: 5
Value of a after post-increment: 6
Pre-increment: 7
Value of a after pre-increment: 7
Post-decrement: 7
Value of a after post-decrement: 6
Pre-decrement: 5
Value of a after pre-decrement: 5

In the above code, we have assigned the value of 5 to variable $a. Then, we have used increment and decrement operators such as post-increment, pre-increment, post-decrement, and pre-decrement to perform different types of operations on this variable. Finally, we have printed the result of each operation using the echo statement.

5. PHP Logical Operators

Logical operators are used to combine two or more conditions. The logical operators are:

OperatorNameExampleResult
&&And$x && $yReturns true if both $x & $y are true.
||Or$x || $yReturns true if any of $x, $y is true.
andAnd$x and $yReturns true if both $x & $y are true.
orOr$x or $yReturns true if any of $x, $y is true.
xorXor$x Xor $yReturns true if either $x or $y is true, but not both.
!Not!$x<dsfReturns true if $x is not true.

Here are some examples of logical operators in PHP:

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

// AND operator
if ($a && $b) {
    echo "AND operator: true<br>";
} else {
    echo "AND operator: false<br>";
}

// OR operator
if ($a || $b) {
    echo "OR operator: true<br>";
} else {
    echo "OR operator: false<br>";
}

// NOT operator
if (!$a) {
    echo "NOT operator: true<br>";
} else {
    echo "NOT operator: false<br>";
}
?>

Output:

AND operator: false
OR operator: true
NOT operator: false

In the above code, we have assigned the boolean value of true to variable $a and the boolean value of false to variable $b. Then, we have used logical operators such as AND, OR, and NOT to perform different types of logical operations on these variables. Finally, we have printed the result of each operation using the echo statement.

6. PHP String Operators

String operators are used to concatenate two or more strings. The string operators are:

  • . Concatenation
  • .= Combined Concatenation

Here are some examples of string operators in PHP:

<?php
$a = "Hello";
$b = "World";

// Concatenation operator
$c = $a . " " . $b;
echo "Concatenation operator: " . $c . "<br>";

// Concatenation assignment operator
$a .= " Everyone";
echo "Concatenation assignment operator: " . $a . "<br>";
?>

Output:

Concatenation operator: Hello World
Concatenation assignment operator: Hello Everyone

In the above code, we have assigned the string value of “Hello” to variable $a and the string value of “World” to variable $b. Then, we have used the concatenation operator . to concatenate these two strings and store the result in variable $c. We have printed the result using the echo statement.

After that, we have used the concatenation assignment operator .= to concatenate the string ” Everyone” to the existing value of variable $a. We have printed the result using the echo statement again.

7. Array Operators

Array operators are used to compare arrays. The array operators are:

  • Union (+)
  • Equality (==)
  • Identity (===)
  • Inequality (!= or <>)
  • Non-identity (!==)

Here are some examples of array operators in PHP:

<?php
// Array 1
$arr1 = array("a" => "apple", "b" => "banana", "c" => "cherry");

// Array 2
$arr2 = array("d" => "date", "e" => "elderberry", "b" => "blueberry");

// Union operator
$union = $arr1 + $arr2;
print_r($union);
echo "<br>";

// Equality operator
if ($arr1 == $arr2) {
    echo "Equality operator: true<br>";
} else {
    echo "Equality operator: false<br>";
}

// Identity operator
if ($arr1 === $arr2) {
    echo "Identity operator: true<br>";
} else {
    echo "Identity operator: false<br>";
}

// Inequality operator
if ($arr1 != $arr2) {
    echo "Inequality operator: true<br>";
} else {
    echo "Inequality operator: false<br>";
}

// Non-identity operator
if ($arr1 !== $arr2) {
    echo "Non-identity operator: true<br>";
} else {
    echo "Non-identity operator: false<br>";
}
?>

Output:

Array ( [a] => apple [b] => banana [c] => cherry [d] => date [e] => elderberry )
Equality operator: false
Identity operator: false
Inequality operator: true
Non-identity operator: true

In the above code, we have created two arrays $arr1 and $arr2. We have used array operators such as union operator +, equality operator ==, identity operator ===, inequality operator !=, and non-identity operator !== to perform different types of operations on these arrays.

The union operator + combines the key-value pairs of both arrays and creates a new array. The equality operator == checks if the two arrays have the same key-value pairs, regardless of their order. The identity operator === checks if the two arrays have the same key-value pairs in the same order. The inequality operator != checks if the two arrays do not have the same key-value pairs, regardless of their order. The non-identity operator !== checks if the two arrays do not have the same key-value pairs in the same order.

Finally, we have printed the result of each operation using the print_r statement and the echo statement.

8. Conditional assignment operators

Conditional assignment operators in PHP are shorthand operators that combine a conditional expression with an assignment. These operators are useful for assigning a value to a variable only if a certain condition is met.

There are two types of conditional assignment operators in PHP:

  1. Ternary operator (?:)
  2. Null coalescing operator (??)

1. Ternary operator (?:)

This operator is a shorthand way of writing an if-else statement. The syntax of the ternary operator is as follows:

$variable = (condition) ? value_if_true : value_if_false;

If the condition is true, the value of $variable will be set to value_if_true, otherwise it will be set to value_if_false.

Here’s an example of the ternary operator in action:

<?php
// Ternary operator
$age = 20;
$isAdult = ($age >= 18) ? "Yes" : "No";
echo "Is the person an adult? " . $isAdult;
?>

Output:

Is the person an adult? Yes

In the above code, we have used the ternary operator to set the value of $isAdult to “Yes” if the value of $age is greater than or equal to 18, and “No” otherwise. We have printed the result using the echo statement.

2. Null coalescing operator (??)

This operator checks if a variable is null, and if it is, assigns a default value. The syntax of the null coalescing operator is as follows:

$variable = $value ?? $default_value;

If $value is not null, the value of $variable will be set to $value, otherwise it will be set to $default_value.

Here’s an example of the null coalescing operator in action:

<?php
// Null coalescing operator
$username = $_POST["username"] ?? "Guest";
echo "Welcome, " . $username;
?>

In the above code, we have used the null coalescing operator to set the value of $username to the value of $_POST["username"] if it is not null, and “Guest” otherwise. We have printed the result using the echo statement.

Wordpress Social Share Plugin powered by Ultimatelysocial
Wordpress Social Share Plugin powered by Ultimatelysocial