Operators
An operator is a symbol that represents a certain action. In
$result=5 * 3;
The * (Multiplier) is an operator.
In PHP (and many other programming languages) you can have different operators.
Assignment operators
=, -=, /=, *=, +=, .=, |=, >>=, <<=, &=, ^=, and &
Assignment operators are used to assign a value to a variable. The most elementary is the = sign that is a simple operator:
$result = 5;
But there are more assignment operators. The mostly used are
.= which concatenates strings like
$result= 'hello t '.$value;
$result.= 'welcome!';
+= which adds a number to the variable.
-= which subtracts a number from the variable.
$result=12;
$result+=1; // adds 1 to $result and is equivalent with $result=$result + 1;
Calculations
We have already seen some operators that perform calculation. As an overview:
+ Add operator
- Subtract operator
* Multiplication operator
/ Division operator (returns the quotient)
% Modulo function (returns the remainder of a division)
The variable type of the result depends on the result. If you have two integers like 6 and 5, and you divide them, then the result will become a float.
There are two operators that can be used for fast increment or decrement.
++ Increase by one
-- decrease by one
Examples;
$a++; $a--;
It is also possible to put the ++ and -- before the variable.
$++a, $--a;
This is rarely used, but can be helpful if the variable is used as a loop variable. It means that the increment of the variable is performed initially before the expression is used.
Comparisons
You will have the need to compare values often. Depending on the comparison you have to execute or skip a block of code. In PHP we have the following compare operators
== Is equal
!= Is not equal
<= Is less equal or equal
< Is less equal
> Is greater than
>= Is greater or equal
The result of a compare is a boolean, always 'true' or 'false'.
The ! operator can be used to turn the compare
if (!$a < $b) { ... }
Needs to be read as if not $a < $b then... That is the same as if($a >= $b).
In this example the use of ! can be easily avoided, but the use can be quite handy when you have to respond to the result of a function. Example:
$var = 10;
$message = ($var>10) ? ‘greater’: ‘smaller or equal’;
echo $message;
will output smaller or equal.
For larger expressions, it is possible that the code will become fuzzy. For that reason you can always fall back to the traditional structure:
if($a < $b) {
some code when true
} else {
code when not true
}
Special comparisson (ternary operator)
A very handy construction to avoid if-then-else (see application flow) constructions is the use of ternary operators. It is mostly used in relative simple expressions:
if(condition) ? { expression 1} : {expression 2} ;
if the condition is true then expression 1 is executed otherwise expression 2 will be executed.
Logical operators
Sometimes you have more complex decision rules. In that case you need to be able to combine arguments with 'and' or 'or'. In PHP there are separate operators for these kinds of comparisons
&& Logical and
|| Logical or
xor (not very commonly used) .
It is mandatory to use () to prioritize the decision:
if( (condition) or (condition) ) {...}
if( ( (condition) or (condition)) and (condition) ) { ...}
To avoid misunderstanding, use brackets for prioritizing. It makes reading a lot easier to others, although there are rules what condition (and/or) comes first. But using brackets is clear to anybody.
Another thing to consider is the sequence of comparison. The execution of a condition is terminated as soon as the outcome is clear. So it you have an or condition and the first condition is true, then the second one will not be determined. If you consider this rule, then you can create your ifs more efficient. Although the computers are very powerful nowadays, it still can become a point of concern when processing much data.
Other operators
There are more operators to work with bits and bytes, but they fall out of scope of this crash course. If you are not an experience developer you will not miss these as they are not widely used.