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
In PHP, what does represent?
That's heredoc syntax. You start a heredoc string by putting <<<
plus a token of your choice, and terminate it by putting only the token (and nothing else!) on a new line. As a convenience, there is one exception: you are allowed to add a single semicolon after the end delimiter.
Example:
echo <<<HEREDOC
This is a heredoc string.
Newlines and everything else is preserved.
HEREDOC;
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 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 operator - mean in PHP?
In your example, $controller
is a PHP object created somewhere, and permissionCheck is a function defined in that object that is being called with the variable $ret
being passed to it. Check this: Reference - What does this symbol mean in PHP?.
What does = mean in PHP?
=>
is the separator for associative arrays. In the context of that foreach loop, it assigns the key of the array to $user
and the value to $pass
.
Example:
$user_list = array(
'dave' => 'apassword',
'steve' => 'secr3t'
);
foreach ($user_list as $user => $pass) {
echo "{$user}'s pass is: {$pass}\n";
}
// Prints:
// "dave's pass is: apassword"
// "steve's pass is: secr3t"
Note that this can be used for numerically indexed arrays too.
Example:
$foo = array('car', 'truck', 'van', 'bike', 'rickshaw');
foreach ($foo as $i => $type) {
echo "{$i}: {$type}\n";
}
// prints:
// 0: car
// 1: truck
// 2: van
// 3: bike
// 4: rickshaw
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.
Related Topics
PHP: Sort an Array by the Length of Its Values
File_Get_Contents(): PHP_Network_Getaddresses: Getaddrinfo Failed: Name or Service Not Known
How to Get Rid of Eval-Base64_Decode Like PHP Virus Files
PHP 5 Strpos() Difference Between Returning 0 and False
PHP How to Get Local Ip of System
Find Total Number of Results in MySQL Query With Offset+Limit
Gcm Notifications Not Receiving When App Is in Background Mode in Ios
Send Post Request Using Volley and Receive in PHP
How to Select First 10 Words of a Sentence
Laravel Update Model With Unique Validation Rule For Attribute
Pdo Error: " Sqlstate[Hy000]: General Error " When Updating Database
Best Way to Automatically Remove Comments from PHP Code
Should I Use Curly Brackets or Concatenate Variables Within Strings