I am looking to expand on my PHP knowledge, and I came across something I am not sure what it is or how to even search for it. I am looking at php.net isset code, and I see isset($_GET['something']) ? $_GET['something'] : ''
I understand normal isset operations, such as if(isset($_GET['something']){ If something is exists, then it is set and we will do something }
but I don't understand the ?, repeating the get again, the : or the ''. Can someone help break this down for me or at least point me in the right direction?
It's commonly referred to as 'shorthand' or the Ternary Operator.
$test = isset($_GET['something']) ? $_GET['something'] : '';
means
if(isset($_GET['something'])) {
$test = $_GET['something'];
} else {
$test = '';
}
To break it down:
$test = ... // assign variable
isset(...) // test
? ... // if test is true, do ... (equivalent to if)
: ... // otherwise... (equivalent to else)
Or...
// test --v
if(isset(...)) { // if test is true, do ... (equivalent to ?)
$test = // assign variable
} else { // otherwise... (equivalent to :)
? is called Ternary (conditional) operator : example
It is called the ternary operator. It is shorthand for an if-else block. See here for an example http://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary
If you want an empty string default then a preferred way is one of these (depending on your need):
$str_value = strval($_GET['something']);
$trimmed_value = trim($_GET['something']);
$int_value = intval($_GET['somenumber']);
If the url parameter something
doesn't exist in the url then $_GET['something']
will return null
strval($_GET['something'])
-> strval(null)
-> ""
and your variable $value
is set to an empty string.
trim()
might be prefered over strval()
depending on code (e.g. a Name parameter might want to use it)intval()
if only numeric values are expected and the default is zero. intval(null)
-> 0
Cases to consider:
...&something=value1&key2=value2
(typical)
...&key2=value2
(parameter missing from url $_GET will return null for it)
...&something=+++&key2=value
(parameter is " "
)
Why this is a preferred approach:
$value = isset($_GET['something']) ? $_GET['something'] : '';
$value=isset($_GET['something'])?$_GET['somthing']:'';
Update Strict mode may require something like this:
$str_value = strval(@$_GET['something']);
$trimmed_value = trim(@$_GET['something']);
$int_value = intval(@$_GET['somenumber']);
What you're looking at is called a Ternary Operator, and you can find the PHP implementation here. It's an if else
statement.
if (isset($_GET['something']) == true) {
thing = isset($_GET['something']);
} else {
thing = "";
}
That's called a ternary operator and it's mainly used in place of an if-else statement.
In the example you gave it can be used to retrieve a value from an array given isset returns true
isset($_GET['something']) ? $_GET['something'] : ''
is equivalent to
if (isset($_GET['something'])) {
$_GET['something'];
} else {
'';
}
Of course it's not much use unless you assign it to something, and possibly even assign a default value for a user submitted value.
$username = isset($_GET['username']) ? $_GET['username'] : 'anonymous'
You have encountered the ternary operator. It's purpose is that of a basic if-else statement. The following pieces of code do the same thing.
Ternary:
$something = isset($_GET['something']) ? $_GET['something'] : "failed";
If-else:
if (isset($_GET['something'])) {
$something = $_GET['something'];
} else {
$something = "failed";
}
In PHP 7 you can write it even shorter:
$age = $_GET['age'] ?? 27;
This means that the $age
variable will be set to the age
parameter if it is provided in the URL, or it will default to 27.
See all new features of PHP 7.
Source: Stackoverflow.com