Reference — What does this symbol mean 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";
}
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:
- Understanding Incrementing
What does @ mean in PHP?
The @
operator tells PHP to suppress error messages, so that they will not be shown.
For instance, using:
$result = mysql_query("this is an invalid query");
would result in a warning being shown, telling you that the MySQL query is invalid, while
$result = @mysql_query("this is still an invalid query");
would not.
Note, however, that this is very bad programming practice as it does not make error disappear, it just hides them, and it makes debugging a heck of a lot worse since you can't see what's actually wrong with your code.
Instead of using @
, you should disable just error_reporting
and display_errors
display_errors
in php.ini
What does double question mark (??) operator mean in PHP
It's the "null coalescing operator", added in php 7.0. The definition of how it works is:
It returns its first operand if it exists and is not NULL; otherwise it returns its second operand.
So it's actually just isset()
in a handy operator.
Those two are equivalent1:
$foo = $bar ?? 'something';
$foo = isset($bar) ? $bar : 'something';
Documentation: http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.coalesce
In the list of new PHP7 features: http://php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op
And original RFC https://wiki.php.net/rfc/isset_ternary
EDIT: As this answer gets a lot of views, little clarification:
1There is a difference: In case of ??
, the first expression is evaluated only once, as opposed to ? :
, where the expression is first evaluated in the condition section, then the second time in the "answer" section.
What is the meaning of |= in php?
This is bitwise OR operator
$var1 |= $var2; is equal to $var1 = $var1 | $var2;
What is : in PHP?
PHP offers an alternative syntax for some of its control structures; namely, if, while, for, foreach, and switch. In each case, the basic form of the alternate syntax is to change the opening brace to a colon (:) and the closing brace to endif;, endwhile;, endfor;, endforeach;, or endswitch;, respectively.
What is = (the 'Spaceship' Operator) in PHP 7?
The <=>
("Spaceship") operator will offer combined comparison in that it will :
Return 0 if values on either side are equal
Return 1 if the value on the left is greater
Return -1 if the value on the right is greater
The rules used by the combined comparison operator are the same as the currently used comparison operators by PHP viz. <
, <=
, ==
, >=
and >
. Those who are from Perl or Ruby programming background may already be familiar with this new operator proposed for PHP7.
//Comparing Integers
echo 1 <=> 1; //output 0
echo 3 <=> 4; //output -1
echo 4 <=> 3; //output 1
//String Comparison
echo "x" <=> "x"; //output 0
echo "x" <=> "y"; //output -1
echo "y" <=> "x"; //output 1
What does the .= operator mean in PHP?
It's the concatenating assignment operator. It works similarly to:
$var = $var . "value";
$x .=
differs from $x = $x .
in that the former is in-place, but the latter re-assigns $x
.
What does PHP operator -{...} mean?
It's to enable access to properties which would be invalid syntax as bare literals. Meaning:
$dbObject->mysql-5.4[0]->credentials
This is invalid/ambiguous syntax. To make clear to PHP that mysql-5.4
is a property and not a property minus a float, you need to use the {'..'}
syntax.
To be exact, ->{..}
enables you to use any expression as the property name. For example:
$dbObject->{ sprintf('%s-%.1f', 'mysql', 5.4) }
Related Topics
Unescape or HTML Decode in Twig (PHP Templating)
How to Maintain Session in Curl in PHP
Single Session Login in Laravel
When and Why Should $_Request Be Used Instead of $_Get/$_Post/$_Cookie
Sending Correct File Size with PHP Download Script
Laravel Pluck Fields from Relations
Href="" Automatically Adds to Current Page Url (In PHP). Can't Figure It Out
Check If String Contains Word in Array
How to Use File_Get_Contents() to Compare Two Files
Codeigniter Assets Folder Best Practice