PHP - Operators

PHP - Operators

When it comes to operators, PHP offers endless possibilities

Maybe "operators" is not a word that you use on a daily basis, but that does not mean you don't use them everyday. We all have been learning about operators since pre-school.

Definition

So, what is an operator? Basically, it is something (generally a symbol which represents an operation) that takes two values and produces another value. The junction of the values, the operator and the result is known as expression. You can also find functions and literals (values that don't change) inside expressions as well.

Operator Types

There are different types of operators:

Unary operators: They operate over a single value. For example we have the "not" logical operator (!) and also the increment operator (++).

<?php 

$flag = false;
$flagInvertedValue = !$flag; #true

$i = 2;
$i++; #Increases the variable to 3

?>

Binary Operators: These operators take 2 values (like addition and subtraction) and return another different value.

<?php

$a = 3;
$b = 4;

$totalAddition = $a + $b; #$totalAddition = 7
$totalSubtraction = $b - $a; #$totalSubtraction = 1

?>

And there is also the Ternary Operator, which I must say it's one of my favorites in PHP. This operator takes three values. It evaluates the first value, checks where the condition it's true or false, and returns one of the other two remaining values.

<?php

$flag = true;
$result = $flag ? 'This is true' : 'This is false'; 
// $result = 'This is true'
?>

Arithmetic Operators

These are some of the most used operators in PHP. They can operate in integers as well as in strings.

<?php

+$a; // Converts $a into a float or integer, if $a is a string
-$a; // Opposite value of $a
$a + $b ; // Addition
$a - $b; // Subtraction
$a * $b; // Multiplication
$a / $b; // Division
$a % $b; // Module
$a ** $b; // Exponentiation

?>

Assignment Operators

The Assignment Operators are used to set values to variables. The most basic assignment operator is "=".

<?php

# We assign 3 to the variable $a
$a = 3;

?>

We can also combine the assignment operator with the arithmetic operators, and get some shortcuts to make our code smaller, as follows:

<?php 

$a += $b; // Equals to $a = $a + $b
$a -= $b; // Equals to $a = $a - $b
$a *= $b; // Equals to $a = $a * $b
$a /= $b; // Equals to $a = $a / $b
$a %= $b; // Equals to $a = $a % $b

?>

Comparison Operators

These operators helps us to compare two values, in different scenarios. They are one of the most used types of operators in PHP.

<?php

$a == $b; // Checks if $a equals $b
$a === $b; // Checks if $a equals $b in value and data type
$a != $b; // Checks if $a is different from $b
$a <> $b; // Checks if $a is different from $b
$a !== $b; // Checks if $a is different from $b in value and data type
?>

We can also evaluate if a value is greater or lesser than another, using these operators:

<?php

$a < $b; // $a is lesser than $b
$a > $b; // $a is greater than $b;
$a <= $b; // $a is lesser than $b or equal as $b
$a >= $b; // $a is greater than $b or equal as $b;
?>

Logical Operators

PHP also has a set of operators that allows us to compare boolean values in order to evaluate certain conditions.

<?php

$a && $b; // Returns true if $a and $b are true
$a || $b; // Returns true if $a or $b are true
$a xor $b; // Returns true if $a is true or $b is true, but not both
!$a; // Returns true if $a is false, and false if $a is true

?>

Increment/Decrement Operators

There is also a set of operators that can assist us while operating integers. These are the increment and decrement operators. Basically, what they do is to operate over a single value and add (or subtract) 1 to it.

<?php

++$a; // Pre-increment, increments 1 and then returns $a
$a++; // Post-increment, returns $a and then increments 1
--$a; // Pre-decrement, decrements 1 and then returns $a
$a--; // Post-decrement, returns $a and then decrements 1

 ?>

Concatenation Operator

When it comes to strings, in PHP we have the concatenation operator (.) which allows us to combine two strings together to form a single string.

<?php

$a = "Hello";
$b = $a . "World!"; // $b equals to "Hello World!"

?>

Null Coalescing operator

This operator works more like a shortcut to evaluate if a variable is set and is different than null. It works like a ternary operator. Let's take a look at an example:

<?php

$username = $name ?? 'no name';
# This equals to
$username = isset($name) ? $name : 'no name';

?>

Cool, so now we have learned about the most common types of operators in PHP. Let's continue in our journey through this wonderful programming languange.