In which order are nested short hand assignments evaluated? [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

PHP: Multiple conditions with NOT Equal (!=) operator is not working [duplicate]

This question already has answers here:
PHP if not equal(!=) and or (||) issue. Why doesnt this work?
(6 answers)
Closed 4 years ago.
Hope u people will be fine.
I’m working on a following code in which i want to use multiple php if conditions with multiple not operators (example is below), but when i execute following php code, it always returns true (mean content in always parenthesis always executed) even no condition is true.
I want to ask what is the problem in following code. Is there any specific syntax or rule for using multiple != operator in php conditions. And i am amazed to see that if i use following code by replacing != operator with == operator it is working fine.
if( $ext!="exe" || $ext!="html" || $ext!="htm" || $ext!="js" || $ext!="iso" || $ext!="zip" || $ext!="rar" )
{ // ececk extension
echo $ext."extension";
}
else{
echo "not match";
}
Waiting for your kind replies. and sorry for my bad english.
Better code:
$allowed = array('jpeg','png');
if(in_array($ext,$allowed)){
echo "Correct";
}
else {
echo "Wrong";
}
User XOR operator instead of OR as or checked all condition need to be identical while XOR return true if anyone from all is identical:
if(!($ext!=="exe") xor ($ext=="html")){ //Add more conditions
//each condition in new brackets & ! sign in start before all conditions
{ // ececk extension
echo $ext."extension";
}
else{
echo "not match";
}
Hope it will help

corresponding nested ternary operator in php? [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'];

Categories