PHP Operators

php-operators-programming-pot
••• PHP Operators

In this tutorial, you will learn about PHP Operators and how they are used in programs alongside their functionalities. An operator is something that takes one or more values (or expressions, in programming jargon) and yields another value (so that the construction itself becomes an expression).

PHP Operators


PHP Operators are symbols that tell the compiler to perform certain mathematical or logical manipulation. In simple terms, we can say operators are used to manipulating Data and Variables.

For example: +,- are the operators used for mathematical calculation.

List of PHP Operators


  1. Arithmetic Operator
  2. Relational Operator
  3. Logical Operator
  4. Assignment Operator
  5. Increment/Decrement Operator
  6. Comparison Operator
  7. Bitwise Operator
  8. String Operators
  9. Array Operators

The data items in which any operation is carried out are called operands.

x + y

Here x and y are operands and + is an operator and calculating sum is an operation.

Operators which require two operands are called binary operators and which takes single operand are called unary operators.

PHP Operators

1)  Arithmetic operators in PHP


Operators used in the arithmetic operation of addition, subtraction, multiplication or division are called arithmetic operators.

Arithmetic operators
Arithmetic operatorsMeaning
+Addition or unary plus
Subtraction or unary minus
*Multiplication
/Division
%Modulus division

2) Relational Operator in PHP


Relational operators are used to comparing two operators depending on their relation.

For example, for comparing the price of two things. The value of a relational operator is either or10. If the specified relation is true then else10.

a > b

Here, > is a relational operator and it will return if 1a is greater than elseb it will return.0

Relational operator
Relational operatorMeaning
<is less than
<=is less than or equal to
>is greater than
>=is greater than or equal to
==is equal to
!=is not equal to
Relational operator
A syntax error will occur if the two symbols in any of the operators ==, >=, <= and != are separated by the space.

3) Logical Operator in PHP


Logical operators are used when more than one condition is tested. They are used to combine more than one relational expressions.

a > b && c == 1

Here && is relational operator used to combine two relational expressions a > b and c == 1.

Logical Operator
Logical OperatorExampleMeaning
AND$a and $bTRUE if both $a and $b are TRUE.
OR$a or $bTRUE if either $a or $b is TRUE.
XOR$a xor $bTRUE if either $a or $b is TRUE, but not both.
NOT! $aTRUE if $a is not TRUE.
AND$a && $bTRUE if both $a and $b are TRUE.
OR$a || $bTRUE if either $a or $b is TRUE.

The reason for the two different variations of “and” and “or” operators is that they operate at different precedences.

4) Assignment Operators in PHP


The basic assignment operator is “=”. Your first inclination might be to think of this as “equal to”. Don’t. It really means that the left operand gets set to the value of the expression on the right (that is, “gets set to”).

The value of an assignment expression is the value assigned. That is, the value of “$a = 3” is 3. This allows you to do some tricky things:

<?php

$a = ($b = 4) + 5; // $a is equal to 9 now, and $b has been set to 4.

?>
Assignment Operators
Assignment OperatorsExampleMeaning
=x = yThe left operand gets set to the value of the expression on the right
+=x = x + yAddition
-=x = x – ySubtraction
*=x = x * yMultiplication
/=x = x / yDivision
%=x = x % yModulus

Note: Don’t get confused between equality operator == and assignment operator =.

5) Bitwise Operator in PHP


Bitwise operators are used to manipulating data at a bit level. They are used for testing or shifting the bit.

Bitwise Operator
Bitwise OperatorMeaning
<bitwise AND
|bitwise OR
^bitwise exclusive OR
<<shift left
>>shift right

6) Increment / Decrement Operator in PHP


PHP provides an increment PHP Operator and ++ decrement PHP Operator. -- The functionality of is to ++ add 1 unit to the operand and is -- to subtract 1 from the operand.\

++ $a;
-- $b;

Here ++a is equivalent to a = a + 1 and –b is equivalent to b = b – 1.

  • There are two kinds of increment and decrement operator i.e prefix and postfix.
  • If the operator is used before the variable i.e ++a then it is called prefix increment operator.
  • If the operator is used after variable i.e a++ then it is called postfix increment operator.
  • In the prefix operator, first 1 is added and then the value is assigned to the variable.
  • In postfix operator, first the value is assigned then only 1 is added and the added value is assigned.
Increment / Decrement Operator
Increment / Decrement OperatorExampleMeaning
Pre-increment++$aIncrements $a by one, then returns $a.
Post-increment$a++Returns $a, then increments $a by one.
Pre-decrement–$aDecrements $a by one, then returns $a.
Post-decrement$a–Returns $a, then decrements $a by one.

Note: The increment/decrement operators only affect numbers and strings. Arrays, objects, and resources are not affected. Decrementing NULL values has no effect too, but incrementing them results in 1.

7) Comparison Operator in PHP


Comparison operators, as their name implies, allow you to compare two values. You may also be interested in viewing the type comparison tables, as they show examples of various type related comparisons.

Comparison Operator
Comparison Operator ExampleMeaning
Equal$a == $bTRUE if $a is equal to $b after type juggling.
Identical$a === $bTRUE if $a is equal to $b, and they are of the same type.
Not equal$a != $bTRUE if $a is not equal to $b after type juggling.
Not equal$a <> $bTRUE if $a is not equal to $b after type juggling.
Not identical$a !== $bTRUE if $a is not equal to $b, or they are not of the same type.
Less than$a < $bTRUE if $a is strictly less than $b.
Greater than$a > $bTRUE if $a is strictly greater than $b.
Less than or equal to$a <= $bTRUE if $a is less than or equal to $b.
Greater than or equal to$a >= $bTRUE if $a is greater than or equal to $b.
Spaceship$a <=> $bAn integer less than, equal to, or greater than zero when $a is respectively less than, equal to, or greater than $b. Available as of PHP 7.

If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. These rules also apply to the switch statement. The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value.

<?php
var_dump(0 == "a"); // 0 == 0 -> true
var_dump("1" == "01"); // 1 == 1 -> true
var_dump("10" == "1e1"); // 10 == 10 -> true
var_dump(100 == "1e2"); // 100 == 100 -> true

switch ("a") {
case 0:
    echo "0";
    break;
case "a": // never reached because "a" is already matched with 0
    echo "a";
    break;
}
?>

8) String Operators in PHP


There are two string operators. The first is the concatenation operator (‘.’), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator (‘.=‘), which appends the argument on the right side to the argument on the left side.

<?php
$a = "Hello ";
$b = $a . "World!"; // now $b contains "Hello World!"

$a = "Hello ";
$a .= "World!";     // now $a contains "Hello World!"
?>

PHP has two operators that are specially designed for strings.

String Operators
String OperatorsExampleMeaning
Concatenation(.)$txt1 . $txt2Concatenation of $txt1 and $txt2
Concatenation assignment(.=)$txt1 .= $txt2Appends $txt2 to $txt1

9) Array Operators in PHP


The PHP array operators are used to compare arrays.

<?php
    $a = array("apple", "banana");
    $b = array(1 => "banana", "0" => "apple");

    var_dump($a == $b); // bool(true)
    var_dump($a === $b); // bool(false)
?>