I use short if in PHP, and when Use this example
$a = 100 ? 200 : 300;
$a
equal 200, but i don't know how below example work.
$a = 100 ? : 300;
after this code $a
equal 100.
Why?
I use short if in PHP, and when Use this example
$a = 100 ? 200 : 300;
$a
equal 200, but i don't know how below example work.
$a = 100 ? : 300;
after this code $a
equal 100.
Why?
In php manual: http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary
Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression
expr1 ?: expr3
returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise
Let me explain so you can understand the differnece.
When you use one = it's assigning value,
When you use == is checking if same value and
When using === is checking value + type also.
So in your example $a = 100 ? 200 : 300;
it is like this:
If $a can be set to 100 (which can) then True (so 200).
In your second example : $a = 100 ? : 300;
Again you are assinging $a to 100, which is true, so it remains 100 as your true condition is empty.