Operators in PHP
Operators take one or more value perform operations and give us output. A simple operator is Sum (+) which takes values and give its addition.
Arithmetic Operators: These are just basic mathmatical opertaors we learned.
<?php $m = 10; $n = 2; echo $m+$n." "; // Addition echo $m-$n." "; // Substraction echo $m*$n." "; // Multiplication echo $m/$n." "; // Division echo $m%$n." "; // Modulus /* Do not enclose expression containing operators in " " This will simply put values of variables and print as literal string. You may see that here*/ echo "$m+$n"; ?>
Assignment Operator(=): Assigns expression value to a variable on the left side of it and don't confuse it with mathmatical 'equal to'.
Assignment can be by value or by reference which we have already discussed. But keep in mind that Assign by refrence will only work for named variables as it uses memory location and only named variables are allocated memory space which can be reterived later.
Bitwise Operators: Bitwise Operators are the operataors which works with the bits i.e. these operators manipulates the bits. For example
~$a will set the bits which are not set. As this opertaor is manipulating bitwise its a bitwise operator.
Comparision Operators: Comparision Operataors gives Boolean(With exception) as output after comparing the values.
<?php $a = 5; // Assignment Operator $b = $a; // Assignment by value $c = &$a; // Assignment by Reference echo var_dump($a = 6)." "; // = Assignment echo var_dump($a == 5)." "; // == Comparision Opertaor echo $b." ".$c; // $b by value and $c by ref ?>
After seeing this example you will understand the difference between
= operator assigns value while
== give boolean(i.e. compares) and also assignment by value and assignment by reference.
Error control operator(@): PHP provides an error control operator. After putting
@ before any exression, error thrown by that expression will be ignored (PHP wil not report that error).
Incrementing/Decrementing Operator: Simply increments or decrements values.
<?php $a = 5; echo ++$a." "; //increments and then return value echo $a++." "; //retun value then increment echo --$a." "; //decrements and then retun value echo $a--." "; //return value then increments echo $a; ?>
You can understand the difference in pre-increment/decrement
++$a and post-increment/decrement
$a++. Just keep in mind that the value returend will be printed by
Logical Operators: Logical opertaors also return Boolean and are the most useful operators after arithmetic operators.
$x and $y- True only if both
$x or $y- True if either one is true
!$x(Not) - True if
$xis not true
$x&&$y- True if both are true
$x||$y- True if either one is true
String Operators: PHP provides two string operators
. concatenate two strings and
.= concatenate string to itself i.e.
$a .= $b is equivalent to
$a = $a.$b
<?php $a = "First"; $b = $a."Second"; // concatenate echo $b; $a .= $b; echo $a; ?>
When we have multiple operators in an expression precedence will decide which operator to be solved first. Operator of higher precedence will be solved first.
5+6*2 is equal to
* has higher precedence thatn
+. So we can write that as
NOTE: While using
or operators keep in mind that these have lower precedence than
= so results might shock you. So try to use
// as these have higher precedence.
<?php echo 2+3*7; $a = true and false; // equivalent to ($a = true) and false $b = true && false; // equivalent to $b = true && false echo var_dump($a); echo var_dump($b); ?>