In this post we’ll understand the references to symbols in PHP.
Incrementing / Decrementing Operators
++
increment operator
--
decrement operator
Example Name Effect
---------------------------------------------------------------------
++$a Pre-increment Increments $a by one, then returns $a.
$a++ Post-increment Returns $a, then increments $a by one.
--$a Pre-decrement Decrements $a by one, then returns $a.
$a-- Post-decrement Returns $a, then decrements $a by one.
These can go before or after the variable.
If put before the variable, the increment/decrement operation is done to the variable first then the result is returned. If put after the variable, the variable is first returned, then the increment/decrement operation is done.
For example:
$apples = 10;
for ($i = 0; $i < 10; ++$i) {
echo 'I have ' . $apples-- . " apples. I just ate one.\n";
}
In the case above ++$i
is used, since it is faster. $i++
would have the same results.
Pre-increment is a little bit faster because it really increments the variable and after that ‘returns’ the result. Post-increment creates a special variable, copies there the value of the first variable and only after the first variable is used, replaces its value with second’s.
However, you must use $apples--
, since first, you want to display the current number of apples, and then you want to subtract one from it.
You can also increment letters in PHP:
$i = "a";
while ($i < "c") {
echo $i++;
}
Once z
is reached aa
is next, and so on.
Note that character variables can be incremented but not decremented and even so only plain ASCII characters (a-z and A-Z) are supported.
Bitwise Operator
What is a bit? A bit is a representation of 1 or 0. Basically OFF(0) and ON(1)
What is a byte? A byte is made up of 8 bits and the highest value of a byte is 255, which would mean every bit is set. We will look at why a byte’s maximum value is 255.
-------------------------------------------
| 1 Byte ( 8 bits ) |
-------------------------------------------
|Place Value | 128| 64| 32| 16| 8| 4| 2| 1|
-------------------------------------------
This representation of 1 Byte
1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 = 255 (1 Byte)
A few examples for better understanding
The “AND” operator: &
$a = 9;
$b = 10;
echo $a & $b;
This would output the number 8. Why? Well let’s see using our table example.
-------------------------------------------
| 1 Byte ( 8 bits ) |
-------------------------------------------
|Place Value | 128| 64| 32| 16| 8| 4| 2| 1|
-------------------------------------------
| $a | 0| 0| 0| 0| 1| 0| 0| 1|
-------------------------------------------
| $b | 0| 0| 0| 0| 1| 0| 1| 0|
-------------------------------------------
| & | 0| 0| 0| 0| 1| 0| 0| 0|
-------------------------------------------
So you can see from the table the only bit they share together is the 8 bit.
Second example
$a = 36;
$b = 103;
echo $a & $b; // This would output the number 36.
$a = 00100100
$b = 01100111
The two shared bits are 32 and 4, which when added together return 36.
The “Or” operator: |
$a = 9;
$b = 10;
echo $a | $b;
This would output the number 11. Why?
-------------------------------------------
| 1 Byte ( 8 bits ) |
-------------------------------------------
|Place Value | 128| 64| 32| 16| 8| 4| 2| 1|
-------------------------------------------
| $a | 0| 0| 0| 0| 1| 0| 0| 1|
-------------------------------------------
| $b | 0| 0| 0| 0| 1| 0| 1| 0|
-------------------------------------------
| | | 0| 0| 0| 0| 1| 0| 1| 1|
-------------------------------------------
You will notice that we have 3 bits set, in the 8, 2, and 1 columns. Add those up: 8+2+1=11.
<=>
Spaceship Operator
Added in PHP 7
The spaceship operator <=>
is the latest comparison operator added in PHP 7. It is a non-associative binary operator with the same precedence as equality operators (==
, !=
, ===
, !==
). This operator allows for simpler three-way comparison between left-hand and right-hand operands.
The operator results in an integer expression of:
0
when both operands are equal- Less than
0
when the left-hand operand is less than the right-hand operand - Greater than
0
when the left-hand operand is greater than the right-hand operand
e.g.
1 <=> 1; // 0
1 <=> 2; // -1
2 <=> 1; // 1
A good practical application of using this operator would be in comparison type callbacks that are expected to return a zero, negative, or positive integer based on a three-way comparison between two values. The comparison function passed to usort
is one such example.
Before PHP 7 you would write…
$arr = [4,2,1,3];
usort($arr, function ($a, $b) {
if ($a < $b) {
return -1;
} elseif ($a > $b) {
return 1;
} else {
return 0;
}
});
Since PHP 7 you can write…
$arr = [4,2,1,3];
usort($arr, function ($a, $b) {
return $a <=> $b;
// return $b <=> $a; // for reversing order
});
The underscore character ‘_’ as in _()
is an alias to the gettext()
function.
Understanding the operators/symbols using this table.
Syntax | Name | Description |
---|---|---|
x == y | Equality | true if x and y have the same key/value pairs |
x != y | Inequality | true if x is not equal to y |
x === y | Identity | true if x and y have the same key/value pairsin the same order and of the same types |
x !== y | Non-identity | true if x is not identical to y |
x <=> y | Spaceship | 0 if x is equal to y, greater than 0 if x > y, less than 0 if x < y |
++x | Pre-increment | Increments x by one, then returns x |
x++ | Post-increment | Returns x, then increments x by one |
--x | Pre-decrement | Decrements x by one, then returns x |
x-- | Post-decrement | Returns x, then decrements x by one |
x and y | And | true if both x and y are true . If x=6, y=3 then(x < 10 and y > 1) returns true |
x && y | And | true if both x and y are true . If x=6, y=3 then(x < 10 && y > 1) returns true |
x or y | Or | true if any of x or y are true . If x=6, y=3 then(x < 10 or y > 10) returns true |
x || y | Or | true if any of x or y are true . If x=6, y=3 then(x < 3 || y > 1) returns true |
a . b | Concatenation | Concatenate two strings: “Hi” . “Ha” |
Magic constants: Although these are not just symbols but important part of this token family. There are eight magical constants that change depending on where they are used.
__LINE__
: The current line number of the file.
__FILE__
: The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, __FILE__
always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.
__DIR__
: The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__)
. This directory name does not have a trailing slash unless it is the root directory. (Added in PHP 5.3.0.)
__FUNCTION__
: The function name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the function name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.
__CLASS__
: The class name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the class name as it was declared (case-sensitive). In PHP 4 its value is always lowercased. The class name includes the namespace it was declared in (e.g. Foo\Bar
). Note that as of PHP 5.4 __CLASS__
works also in traits. When used in a trait method, __CLASS__
is the name of the class the trait is used in.
__TRAIT__
: The trait name. (Added in PHP 5.4.0) As of PHP 5.4 this constant returns the trait as it was declared (case-sensitive). The trait name includes the namespace it was declared in (e.g. Foo\Bar
).
__METHOD__
: The class method name. (Added in PHP 5.0.0) The method name is returned as it was declared (case-sensitive).
__NAMESPACE__
: The name of the current namespace (case-sensitive). This constant is defined in compile-time (Added in PHP 5.3.0).
Type Operators
instanceof
is used to determine whether a PHP variable is an instantiated object of a certain class.
<?php
class mclass { }
class sclass { }
$a = new mclass;
var_dump($a instanceof mclass);
var_dump($a instanceof sclass);
The above example will output:
bool(true)
bool(false)
Reason: Above Example $a
is a object of the mclass
so use only a mclass
data not instance of with the sclass
Example with inheritance
<?php
class pclass { }
class childclass extends pclass { }
$a = new childclass;
var_dump($a instanceof childclass);
var_dump($a instanceof pclass);
The above example will output:
bool(true)
bool(true)
Example with Clone
<?php
class cloneable { }
$a = new cloneable;
$b = clone $a;
var_dump($a instanceof cloneable);
var_dump($b instanceof cloneable);
The above example will output:
bool(true)
bool(true)
An overview of operators in PHP:
Logical Operators:
- $a && $b : TRUE if both $a and $b are TRUE.
- $a || $b : TRUE if either $a or $b is TRUE.
- $a xor $b : TRUE if either $a or $b is TRUE, but not both.
- ! $a : TRUE if $a is not TRUE.
- $a and $b : TRUE if both $a and $b are TRUE.
- $a or $b : TRUE if either $a or $b is TRUE.
Comparison operators:
- $a == $b : TRUE if $a is equal to $b after type juggling.
- $a === $b : TRUE if $a is equal to $b, and they are of the same type.
- $a != $b : TRUE if $a is not equal to $b after type juggling.
- $a <> $b : TRUE if $a is not equal to $b after type juggling.
- $a !== $b : TRUE if $a is not equal to $b, or they are not of the same type.
- $a < $b : TRUE if $a is strictly less than $b.
- $a > $b : TRUE if $a is strictly greater than $b.
- $a <= $b : TRUE if $a is less than or equal to $b.
- $a >= $b : TRUE if $a is greater than or equal to $b.
- $a <=> $b : An 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.
- $a ? $b : $c : if $a return $b else return $c (ternary operator)
- $a ?? $c : Same as $a ? $a : $c (null coalescing operator – requires PHP>=7)
Arithmetic Operators:
- -$a : Opposite of $a.
- $a + $b : Sum of $a and $b.
- $a – $b : Difference of $a and $b.
- $a * $b : Product of $a and $b.
- $a / $b : Quotient of $a and $b.
- $a % $b : Remainder of $a divided by $b.
- $a ** $b : Result of raising $a to the $b’th power (introduced in PHP 5.6)
Incrementing/Decrementing Operators:
- ++$a : Increments $a by one, then returns $a.
- $a++ : Returns $a, then increments $a by one.
- –$a : Decrements $a by one, then returns $a.
- $a– : Returns $a, then decrements $a by one.
Bitwise Operators:
- $a & $b : Bits that are set in both $a and $b are set.
- $a | $b : Bits that are set in either $a or $b are set.
- $a ^ $b : Bits that are set in $a or $b but not both are set.
- ~ $a : Bits that are set in $a are not set, and vice versa.
- $a << $b : Shift the bits of $a $b steps to the left (each step means “multiply by two”)
- $a >> $b : Shift the bits of $a $b steps to the right (each step means “divide by two”)
String Operators:
- $a . $b : Concatenation of $a and $b.
Array Operators:
- $a + $b : Union of $a and $b.
- $a == $b : TRUE if $a and $b have the same key/value pairs.
- $a === $b : TRUE if $a and $b have the same key/value pairs in the same order and of the same types.
- $a != $b : TRUE if $a is not equal to $b.
- $a <> $b : TRUE if $a is not equal to $b.
- $a !== $b : TRUE if $a is not identical to $b.
Assignment Operators:
- $a = $b : The value of $b is assigned to $a
- $a += $b : Same as $a = $a + $b
- $a -= $b : Same as $a = $a – $b
- *$a = $b : Same as $a = $a * $b
- $a /= $b : Same as $a = $a / $b
- $a %= $b : Same as $a = $a % $b
- **$a = $b : Same as $a = $a ** $b
- $a .= $b : Same as $a = $a . $b
- $a &= $b : Same as $a = $a & $b
- $a |= $b : Same as $a = $a | $b
- $a ^= $b : Same as $a = $a ^ $b
- $a <<= $b : Same as $a = $a << $b
- $a >>= $b : Same as $a = $a >> $b
- $a ??= $b : The value of $b is assigned to $a if $a is null or not defined (null coalescing assignment operator – requires PHP>=7.4)
Hope you learned something from this post.
Follow Programming Articles for more!