corresponding nested ternary operator in php? [duplicate] - php

This question already has answers here:
Stacking Multiple Ternary Operators in PHP
(11 answers)
Closed 2 years ago.
I want to convert following if else condition to nested ternary operator.
if ($projectURL) {
echo $projectURL;
} elseif ($project['project_url']) {
echo $project['project_url'];
} else {
echo $project['project_id'];
}
I have written like following.
echo ($projectURL)?$projectURL:($project['project_url'])?$project['project_url']: $project['project_id'];
But it is found as not working properly.Is this not a right way?

Ternary operators are tricky thing in PHP, as they are left-associative (unlike all other languages, where it's right-associative). You will need to use parenthesis to tell PHP what you want exactly in this case:
echo ($projectURL ? $projectURL : ($project['project_url'] ? $project['project_url'] : $project['project_id']));

As of php 7 we can use Null coalescing operator
echo $projectURL ?? $project['project_url'] ?? $project['project_id'];

Related

How to understand isset in codeigniter(php)? [duplicate]

This question already has answers here:
What are the PHP operators "?" and ":" called and what do they do?
(10 answers)
How to check if $data variable is set with Codeigniter?
(6 answers)
Closed 4 years ago.
Hey I'm new to php and codeigniter. I know codeigniter has an isset function.
what does the following code mean? Can someone please help
<?php echo isset($error) ? $error : ''; ?>
isset is a php function, you can use it without CodeIgnitor, but it basically checks to see if the variable has been set yet.
$someVariable = 'This variable has been set';
var_dump(isset($someVariable)); // True
var_dump(isset($anotherVariable)); // False
the ? and : parts tell PHP what to do. It's called a ternary operator, and can be thought as as a short if statement:
echo isset($someVariable) ? 'set' : 'not set';
is the same as:
if (isset($someVariable)) {
echo 'set';
} else {
echo 'not set';
}
http://php.net/manual/en/function.isset.php
http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary

How to use if and else inside echo statement? [duplicate]

This question already has answers here:
How to write a PHP ternary operator [duplicate]
(10 answers)
Closed 5 years ago.
I've heard something about ternary operator but I kind of beginner and don't know how to use it, I'm trying to make this:
echo '<div>
'.if(LoginCheck($this->db) == true):.'
<span class="option1"></span>
'.else:.'
<span class="option1"></span>
'.endif;.'
</div>';
Yes the ternary operator is what you're looking for, but you have to chain them to get the if-else statement.
echo '<div>'.(LoginCheck($this->db) == true ?
'<span class="option1">' : '<span class="option2"></span>').'</div>'
The evaluation statement comes before the question mark (?) and there is no if tag to put before the statement. It's just the expression to evaluate to true/false, and then the question mark, and then the first statement is if the expression is truthy, the second statement, separated from the first by a colon (:), is if the expression is not truthy.
Look here for some more info.
echo '<div>
'.((LoginCheck($this->db)) ? '
<span class="option1"></span>
' : '
<span class="option1"></span>
').'</div>';
Article about PHP ternary operator
As much i understand you cannot use 'if-else' statement in 'echo'. You have to use reverse. You can use your 'echo' in 'if-else' statement. Below code might help you:
$html = "&ltdiv&gt";
if(LoginCheck($this->db) == true){
$html .= "&ltspan class='option1'>&lt/span>";
}else{
$html .= "&ltspan class='option2'>&lt/span>";
}
$html .= "&lt/div>";
echo $html

Make condition in one line? [duplicate]

This question already has answers here:
PHP conditionals, brackets needed?
(7 answers)
Closed 6 years ago.
Usually I made the if/else condition like this:
if(empty($data['key']))
{
$err = "empty . $key ";
}
but I saw that there is the ternary operator.
I really don't understand how this works.. Someone could show me an example? How can I convert this condition in the ternary logic?
I'm not sure what you're trying to do with your code.
You check if its empty, and then try to set $err to a value by concentrating an empty value.
Perhaps this is more likely what you want.
// Ternary operator
$err = empty($data['key']) ? "empty key" : '';
# -----------IF-----------------THEN ---- ELSE
// Ternary operator (nesting) (not recommended)
// Changing empty() to isset() you must rewrite the entire logic structure.
$err = empty($data) ? 'empty' : is_numeric($data) ? $data : 'not numeric';
# ---------IF--------- THEN -------ELSEIF-----------THEN-----ELSE
// Null Coalescing operator ?? available in PHP 7.
$err = $data['key'] ?? 'empty value';
# ---- IF ISSET USE --- ELSE value
// Nesting
$err = $data['key'] ?? $_SESSION['key'] ?? $_COOKIE['key'] ?? 'no key';
# IF ISSET USE -- IF ISSET USE ------ IF ISSET USE ------ELSE

In which order are nested short hand assignments evaluated? [duplicate]

This question already has answers here:
Stacking Multiple Ternary Operators in PHP
(11 answers)
Closed 2 years ago.
I want to convert following if else condition to nested ternary operator.
if ($projectURL) {
echo $projectURL;
} elseif ($project['project_url']) {
echo $project['project_url'];
} else {
echo $project['project_id'];
}
I have written like following.
echo ($projectURL)?$projectURL:($project['project_url'])?$project['project_url']: $project['project_id'];
But it is found as not working properly.Is this not a right way?
Ternary operators are tricky thing in PHP, as they are left-associative (unlike all other languages, where it's right-associative). You will need to use parenthesis to tell PHP what you want exactly in this case:
echo ($projectURL ? $projectURL : ($project['project_url'] ? $project['project_url'] : $project['project_id']));
As of php 7 we can use Null coalescing operator
echo $projectURL ?? $project['project_url'] ?? $project['project_id'];

PHP is there anyway to do this javascript trick? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php variable = variable1 || variable2
Trying to do this in PHP evaluates to true, instead of returning 'nothing' like js does.
//Javascript
var stuff = false;
document.write(stuff || 'nothing');
So I have to do this. Is there anyway to avoid typing the variable stuff twice?
//PHP
$stuff = false;
echo !empty($stuff)?$stuff:'nothing';
If you use PHP 5.3 or later you can use the shorthand ternary format:
echo ($stuff) ?: 'nothing';

Categories