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
Related
This question already has answers here:
Why check both isset() and !empty()
(10 answers)
Closed 4 years ago.
<?php
function isset_n_empty($var) {
if (isset($var) && !empty($var)){
return true;
}
return false;
}
echo isset_n_empty($x ?? null);
?>
my function is supposed to do the simple if (isset() && !empty()) logic and i used $x ?? null because if it is not used it would give me this error for undefined variables as the example
E_NOTICE : type 8 -- Undefined variable: x -- at line z
is it redundant or well-optimized?
A variable that isn't set is considered empty by empty().
Therefore, if the variable is not set, it is also empty.
Your code could be shortened by using:
echo !empty($x);
I suppose that makes the answer to your question yes.
yes, the isset is redundant. if it's !empty(), then isset() always returns true.
No the function is not redundant. It tells you if your variable is set but also falsey. There's nothing wrong with it.
On the other hand it seems like something that shouldn't really require a function of it's own. Instead of:
echo isset_n_empty($x ?? null);
You can just do:
echo isset($x) && !$x;
It's less code and doesn't require a function.
I hope this will help:
function isset_not_empty($var){
if(!isset($var))
return false;
else
return (!empty($var))
}
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:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 8 years ago.
$action = isset($_GET['action']) ? $_GET['action'] : null;
Im a little unsure what the above line does? I have some vague ideas (and I know what GET does etc) but I have never come across the : "operator"(?) or the ? "operator" (?).
Thanks very much.
it is a short syntax of writing
if(isset($_GET['action'])){
$action = $_GET['action']
}else{
$action = null;
}
That means you check a condition before ? mark, if result is true, you execute the part between ? and :
But if condition check returns false, you execute the part after :
This means simply..
if(isset($_GET['action']))
$action = $_GET['action'];
else
$action = null;
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';
This question already has answers here:
What is ?: in PHP 5.3? [duplicate]
(3 answers)
Closed 8 years ago.
So I'm using the following php code to set variables that are received from a POST method, but I'm interested in how it works.
$var1 = isset($_REQUEST['var1']) ? $_REQUEST['var1'] : 'default';
I understand what it does, but I don't understand the syntax.
Thanks for the help :)
? is just a short and optimised notation of doing this:
if (isset($_REQUEST["var1"])) // If the element "var1" exists in the $_REQUEST array
$var1 = $_REQUEST["var1"]; // take the value of it
else
$var1 = "default"; // if it doesn't exist, use a default value
Note that you might want to use the $_POST array instead of the $_REQUEST array.
This is a short hand IF statement and from that you are assigning a value to $var1
The syntax is :
$var = (CONDITION) ? (VALUE IF TRUE) : (VALUE IF FALSE);
You probably mean ternary operator
Syntax it's same like
if(isset($_REQUEST('var1') ) {
$var1 = ? $_REQUEST('var1')
}else {
$var1 =: 'default';
}
It's the synatx of the ternary operator. It's shorthand for if/else. Please read PHP Manaul
This is a 'ternary operator', what it says is:-
If var1 is set as a post variable then set var1 to that value, otherwise setvar1 to be the string 'default'. Using traditional syntax it would be:-
if (isset($_REQUEST('var1')) { $var1 = $_REQUEST('var1'); } else { $var1 = 'default'; }
its a short way of doing an if. if you are expecting a post variable its must better to use _POST rather than request.
the "?" says if the isset($_REQUEST) is true, then do the everything between the ? and : otherwise do everything between the : and the ;