What is this statement => ($value ? $value : 0) [duplicate] - php

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 2 years ago.
I don't get this operation. Is that a quick if/else statement ?
Inside a HTML :
<?= $LU ? $LU : 0 ?>
Directly in PHP :
$LU ? $LU : 0
I have no idea what it does

This is called Ternary Operator, you can refer to the documentation.
The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1
evaluates to true, and expr3 if expr1 evaluates to false.

Related

Is this '??' is a operator in php or what it's called? Also is it used for empty() or isset() check? [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed last year.
$name = $username ?? '';
I don't know actually what is this ?? called?
?? is Null coalescing Let suppose
$x = expr1 ?? expr2
Returns the value of $x.The value of $x is expr1 if expr1 exists, and is not NULL.
If expr1 does not exist, or is NULL, the value of $x is expr2.Null coalescing Introduced in PHP 7.
Other Examples
<?php
// variable $user is the value of $_GET['user']
// and 'anonymous' if it does not exist
echo $user = $_GET["user"] ?? "anonymous";
echo("<br>");
// variable $color is "red" if $color does not exist or is null
echo $color = $color ?? "red";
?>

PHP Ternary Operators don't work as expected? [duplicate]

This question already has answers here:
Stacking Multiple Ternary Operators in PHP
(11 answers)
Closed 4 years ago.
I have a method that I'm checking if param is null, but if I use a ternary operator to make sure the false result isn't a string, I don't get the same expected result... I am a full stack .NET dev by day, but do some PHP free lance and this just stumped me...
$param = null;
// $active evaluates to true
$active = is_null($param) ? true : false;
// $active evaluates to false
$active = is_null($param) ? true : is_string($param)
? (strtolower($param) === 'true')
: true;
I have used nested ternary operators in C# and JavaScript what feels like countless times, but I don't know if I have ever tried in PHP... does PHP attempt to evaluate all nested ternary operations prior to expressing a result or is there something I'm missing here since from my understanding in this case, the ternary operator should be short circuited and evaluated to true in both circumstances.
The ternary operator is left associative unlike most other languages such as C#. The code:
$active = is_null($param)
? true
: is_string($param)
? (strtolower($param) === 'true')
: true;
is evaluated as follows:
$active = ((is_null($param) ? true : is_string($param))
? (strtolower($param) === 'true') : true);
You must explicitly add parenthesis to make sure ?: works the way it does in familiar languages:
$active = is_null($param)
? true
: (is_string($param)
? (strtolower($param) === 'true')
: true);
You need to wrap your second ternary condition with parenthesis (),
<?php
$param = null;
// $active evaluates to true
$active = is_null($param) ? true : false;
echo "Simple ternary result = $active".PHP_EOL;
// $active evaluates to true
$active = is_null($param) ? true : (is_string($param)? (strtolower($param) === 'true'): true);
echo "Nested ternary result = $active";
?>
Note:
It is recommended that you avoid "stacking" ternary expressions. PHP's
behaviour when using more than one ternary operator within a single
statement is non-obvious:
See Example #4 here at http://php.net/manual/en/language.operators.comparison.php
Example #4 Non-obvious Ternary Behaviour
<?php
// on first glance, the following appears to output 'true'
echo (true?'true':false?'t':'f');
// however, the actual output of the above is 't'
// this is because ternary expressions are evaluated from left to right
// the following is a more obvious version of the same code as above
echo ((true ? 'true' : false) ? 't' : 'f');
// here, you can see that the first expression is evaluated to 'true', which
// in turn evaluates to (bool)true, thus returning the true branch of the
// second ternary expression.
?>
DEMO: https://3v4l.org/gW8pk
This is a well-known problem with PHP. I doubt it will ever be fixed. Use parentheses, or if..else or switch statements, to get the behaviour you want.
(In technical terms, the ternary operator in PHP is "left associative", while that in every other language with this operator is "right associative". The latter is the more logical behaviour for this operator.)

Unable to use AND to replace && in a PHP ternary operator condition [duplicate]

This question already has answers here:
PHP : Difference between '&&' and 'AND' [duplicate]
(1 answer)
'AND' vs '&&' as operator
(10 answers)
Closed 7 years ago.
[update]
This isn't a duplicate question of "what's the difference between AND and &&" rather it's a question regarding the use of AND and the ternary operator. anyway i think i got my answer i didn't know that all operators have precedence which alex kindly pointed out. http://php.net/manual/en/language.operators.precedence.php
[original]
echo TRUE && TRUE ? 'A' : 'B';
// Outputs: A
echo TRUE AND TRUE ? 'A' : 'B';
// Outputs: 1
I know that the ternary operator syntax is
condition ? true : false
and the difference between && and AND is evaluation precedence (of the condition)
e.g. TRUE AND TRUE && TRUE is evaluated at (TRUE AND (TRUE && TRUE))
so shouldn't using both AND and && yield the same return? is this a PHP bug? (im using ver5.5.23)
or should i just accept the fact that if i used AND in the condition it actually becomes excluded from the condition
e.g.
TRUE AND TRUE ? 'A' : 'B' => (TRUE AND (TRUE ? 'A' : 'B'))

Php a line need to underseand [duplicate]

This question already has answers here:
What are the PHP operators "?" and ":" called and what do they do?
(10 answers)
Closed 9 years ago.
what is the meaning of this line? Can any body help me? I have confusion on last 2 sign :''
why without this two sign browser make error? Thanks all.
isset($_POST['but'])? $_POST['but']:'';
U use the tenary comparison operator
A ternary operator have value for true and false
($contidition) ? true : false;
Please refer php documentation about ternary operator comparison operator
In case of:
isset( $_POST['but'] ) ? $_POST['but'] : ''
What it mean is, when $_POST['but'] exist, use it, otherwise use empty string
If u use php version > 5.3 u can use something like
isset($_POST['but']) ? : ''
It's just another way to write if/else statement.
For example:
$but = isset($_POST['but'])? $_POST['but']:'';
Would be the same as:
if(isset($_POST['but'])){
$but= $_POST['but'];
}else{
$but = '';
}
If $_POST request body contains but parameter - use it, overwise use empty string.
If you are misunderstood statement ? something : anything code - it's the Ternary Operator
It's nothing but the Ternary Operator
Explanation:
If your $_POST['but'] is set , then it will assign the same value to it , else it will set a ''
A clear example here
Well it's called an ternary operator.
Example:
$value = 5;
($value > 2) ? true : false; // returns true
The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.
(isset($_POST['but'])) ? $_POST['but']:'';
Here if your value $_POST['but'] is set then it will return $_POST['but'] else it will return ''.

php var structure not clear

what does this structure mean:
$var = isset($var_1) ? $var_1 : $var_2;
I came across it and of course with other values, not $va, $var_1 and $var_2.
thanks.
This is the ternary operator, and means the same as:
if (isset($var_1)) {
$var = $var_1;
}
else {
$var = $var_2;
}
The ternary operator provides a short-hand method of creating simple if/else statements.
It has some syntax errors, correctly:
$var = isset($var_1) ? $var_1 : $var_2;
This means:
if (isset($var_1))
{
$var = $var_1;
}
else
{
$var = $var_2;
}
That means:
if(isset($var_1))
$var = $var_1;
else
$var = $var_2;
It is short syntax for that.
just for your information from the php manual i copy pasted good things to know about ternary comparision operators
The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.
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.
Note: Please note that the ternary operator is a statement, and that it doesn't evaluate to a variable, but to the result of a statement. This is important to know if you want to return a variable by reference. The statement return $var == 42 ? $a : $b; in a return-by-reference function will therefore not work and a warning is issued in later PHP versions.
Note:
It is recommended that you avoid "stacking" ternary expressions. PHP's behaviour when using more than one ternary operator within a single statement is non-obvious:
Example #3 Non-obvious Ternary Behaviour
<?php
// on first glance, the following appears to output 'true'
echo (true?'true':false?'t':'f');
// however, the actual output of the above is 't'
// this is because ternary expressions are evaluated from left to right
// the following is a more obvious version of the same code as above
echo ((true ? 'true' : false) ? 't' : 'f');
// here, you can see that the first expression is evaluated to 'true', which
// in turn evaluates to (bool)true, thus returning the true branch of the
// second ternary expression.
?>

Categories