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 = "<div>";
if(LoginCheck($this->db) == true){
$html .= "<span class='option1'></span>";
}else{
$html .= "<span class='option2'></span>";
}
$html .= "</div>";
echo $html
Related
This question already has answers here:
How to replace "if" statement with a ternary operator ( ? : )?
(16 answers)
Closed 3 years ago.
$n = isset($_GET["n"]) ? $_GET['n'] : '';
I find this "method" to avoid errors before insert stuff in the input type.. and it works.. but I would like a detailed explanation of this line. Thank you!
This is called the ternary operator shorthand of if...else
(condition) ? true : false
There is a condition which has been checked on left, if its true the statement after the ? will be execute else the statement after the : will execute.
It's called Ternary operator
The ternary operator is a shorthand for the if {} else {} structure. Instead of writing this:
if ($condition) {
$result = 'foo'
} else {
$result = 'bar'
}
You can write this:
$result = $condition ? 'foo' : 'bar';
If this $condition evaluates to true, the lefthand operand will be assigned to $result. If the condition evaluates to false, the righthand will be used.
In your case
If the value of $_GET["n"] isset then it'll take $_GET["n"] value.
if the value is not set then it'll take ('') value.
$n = isset($_GET["n"]) ? $_GET['n'] : '';
This question already has answers here:
Troubleshooting "Unexpected T_ECHO" in ternary operator statement
(4 answers)
Closed 3 years ago.
This ternary operator within an element is giving an error:
<div class="col-sm<?php $columnCasecheck === true ? echo '-3' : echo '-4' ?>">
PhpStorm is expecting a colon after true ? and a semicolon after '-3'. Why is that? It seems like a valid ternary operator to me. See screenshot.
Try this:
<?php echo $columnCasecheck === true ? '-3' : '-4'; ?>
Inside the ternary you should put value or expression, instead of commands.
So the echo should be outside the ternary expression.
Also, if you don't need strict comparison, you can just write:
<?php echo $columnCasecheck ? '-3' : '-4'; ?>
So your whole line will be:
<div class="col-sm<?php echo $columnCasecheck ? '-3' : '-4'; ?>">
Take your echo out of an expression as follows -
<?php echo $columnCasecheck === true ? '-3' : '-4'; ?>
Since you want to output the result, just put the echo in front of the expression:
echo $columnCasecheck === true ? '-3' : '-4';
How about this?
<?php $columnCasecheck = $columnCasecheck === true ? '-3' : '-4'; ?>
<div class="col-sm<?=$columnCasecheck?>>value</div>
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'];
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'];
Does anybody know if there is a shortcut for the following statement in PHP?
$output = isset($some_value) ? $some_value : "Some Value Not Set";
echo $output;
This something that I often run into, where $some_value is actually very long and possibly involves a function, such as:
$output = $this->db->get_where('my_db',array('id'=>$id))->row()->some_value) ? $this->db->get_where('my_db',array('id'=>$id))->row()->some_value) : "Some Value Not Set";
echo $output;
It seems that there should be an operator or function that does this. I could easily write one, and I am not looking for that answer, but rather if anybody knows of a built-in shortcut.
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.
http://php.net/manual/en/language.operators.comparison.php
if you need to reuse the long expression from the test after the ?, you can assign it to a variable inside the test (because assignments are expressions returning the assigned value) and use this variable after the ?:
$output = ($some_value = $this->db->get_where('my_db', array('id' => $id))->row()->some_value))
? $some_value
: "Some Value Not Set";
echo $output;
You should be setting a variable with the results of your database call before using the conditional operator for this purpose. Your example makes the database call twice.
For example:
$output = $this->db->get_where('my_db',array('id'=>$id))->row()->some_value);
$output = $output ? $output : "Some Value Not Set";
echo $output;
And with that established, this is a good case where it's really wiser to not use the conditional operator, which really isn't meant to be used as a general purpose if-then shortcut.
You seem to be afraid of whitespace. Use it! Liberally! Your code is much eaiser to read if you add a space before and after the question mark and the colon, respectively. If your statements get too long, add a newline. Try it, it won't hurt you.
I do believe that the conditional operator is the shortcut :) For the sake of saving function calls and readability, I suggest saving the value to a variable first.
$some_value = $this->db->get_where('my_db',array('id'=>$id))->row()->some_value);
$output = $some_value ? $some_value : "Some Value Not Set";
echo $output;
Best way is to:
$output = $this->db->get_where('my_db',array('id'=>$id))->row()->some_value)
echo $output =($output)?$output:"Some Value Not Set";
Only executes once then!