What are the PHP operators ? and : called and what do they do?
This is the conditional operator.
$x ? $y : $z
means "if $x
is true, then use $y
; otherwise use $z
".
It also has a short form.
$x ?: $z
means "if $x
is true, then use $x
; otherwise use $z
".
People will tell you that ?:
is "the ternary operator". This is wrong. ?:
is a ternary operator, which means that it has three operands. People wind up thinking its name is "the ternary operator" because it's often the only ternary operator a given language has.
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 do the '&=' and '=&' operators do?
=&
assigns by reference
$a = 1;
$b =& $a;
$a++;
echo $b; // 2
From PHP Manual on References:
References in PHP are a means to access the same variable content by different names.
&=
is a bitwise AND assignment
$a = 1;
$a &= 1; // is the same as
$a = $a & 1;
echo $a; // 1
From Wikipedia on Bitwise AND:
A bitwise AND takes two binary representations of equal length and performs the logical AND operation on each pair of corresponding bits. In each pair, the result is 1 if the first bit is 1 AND the second bit is 1. Otherwise, the result is 0. For example:
0101
AND 0011
= 0001
EDIT: For a practical example on bitwise operations, see my answer to Bitwise Operations in PHP
What does ? ... : ... do?
This is a ternary operator:
The expression (expr1) ? (expr2) : (expr3)
evaluates to expr2
if expr1
evaluates to TRUE
, and expr3
if expr1
evaluates to FALSE
.
What do the =& and &= operators in PHP mean?
$a &= $b
is short for $a = $a & $b
which is the bitwise-and operator.
$a =& $b
assigns $a as a reference to $b.
How to replace if statement with a ternary operator ( ? : )?
The
(condition) ? /* value to return if condition is true */
: /* value to return if condition is false */ ;
syntax is not a "shorthand if" operator (the ?
is called the conditional operator) because you cannot execute code in the same manner as if you did:
if (condition) {
/* condition is true, do something like echo */
}
else {
/* condition is false, do something else */
}
In your example, you are executing the echo
statement when the $address
is not empty. You can't do this the same way with the conditional operator. What you can do however, is echo
the result of the conditional operator:
echo empty($address['street2']) ? "Street2 is empty!" : $address['street2'];
and this will display "Street is empty!" if it is empty, otherwise it will display the street2 address.
What could be the output of echo ('True'?(true?'t':'f'):'False'); And explain why?
This will echo t.
Because of first it will check the first condition that will give true.
and after that in next condition it again give true and execute the first condition that is t.
In if and else condition it will be write as follow:
if('True') { //condition true and go to in this block
if(true){ //condition true and go to in this block
echo 't'; // echo t
} else {
echo 'f';
}
} else {
echo 'False';
}
How do you use the ? : (conditional) operator in JavaScript?
This is a one-line shorthand for an if-else statement. It's called the conditional operator.1
Here is an example of code that could be shortened with the conditional operator:
var userType;
if (userIsYoungerThan18) {
userType = "Minor";
} else {
userType = "Adult";
}
if (userIsYoungerThan21) {
serveDrink("Grape Juice");
} else {
serveDrink("Wine");
}
This can be shortened with the ?:
like so:
var userType = userIsYoungerThan18 ? "Minor" : "Adult";
serveDrink(userIsYoungerThan21 ? "Grape Juice" : "Wine");
Like all expressions, the conditional operator can also be used as a standalone statement with side-effects, though this is unusual outside of minification:
userIsYoungerThan21 ? serveGrapeJuice() : serveWine();
They can even be chained:
serveDrink(userIsYoungerThan4 ? 'Milk' : userIsYoungerThan21 ? 'Grape Juice' : 'Wine');
Be careful, though, or you will end up with convoluted code like this:
var k = a ? (b ? (c ? d : e) : (d ? e : f)) : f ? (g ? h : i) : j;
1 Often called "the ternary operator," but in fact it's just a ternary operator [an operator accepting three operands]. It's the only one JavaScript currently has, though.
What is the - PHP operator called?
The official name is "object operator" - T_OBJECT_OPERATOR.
Related Topics
How to Use Multiple Databases in Laravel
Extract a Single (Unsigned) Integer from a String
Convert a PHP Object to an Associative Array
Detect Encoding and Make Everything Utf-8
Should I Manually Check For Errors When Calling "MySQLi_Stmt_Prepare"
Finding Cartesian Product With PHP Associative Arrays
Compare 2-Dimensional Data Sets Based on a Specified Second Level Value
How to Implement Authorization Using a Telegram API
How to Create a Comma-separated List from an Array in PHP
When Should I Use 'Self' Over '$This'
How to Get the Last Inserted Id of a MySQL Table in PHP
How to Extract Img Src, Title and Alt from HTML Using PHP
Laravel 5 - Remove Public from Url
How to Use Store and Use Session Variables Across Pages