What are references in PHP? In PHP, references are “aliases” that allow two different variables to read and write a single value.
It makes PHP suppress any error messages (notice, warning, fatal, etc) generated by the associated expression. It works just like a unary operator, for example, it has a precedence and associativity.
What is => in PHP? This is referred to as the double arrow operator. It is an assignment operator used in the creation of associative arrays. It is placed in between the array key and its value. It assigns to the key (what is on its left enclosed quotes), the value of what is on its right side.
passing argument through reference (&$) and by $ is that when you pass argument through reference you work on original variable, means if you change it inside your function it's going to be changed outside of it as well, if you pass argument as a copy, function creates copy instance of this variable, and work on this ...
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";
}
Live example
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.
Stack Overflow Posts:
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 = 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.
|
$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 OperatorThe 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 equal0
when the left-hand operand is less than the right-hand operand0
when the left-hand operand is greater than the right-hand operande.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.
$arr = [4,2,1,3];
usort($arr, function ($a, $b) {
if ($a < $b) {
return -1;
} elseif ($a > $b) {
return 1;
} else {
return 0;
}
});
$arr = [4,2,1,3];
usort($arr, function ($a, $b) {
return $a <=> $b;
// return $b <=> $a; // for reversing order
});
_
Alias for gettext()
The underscore character '_' as in _()
is an alias to the gettext()
function.
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).
Source
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With