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?
asked Aug 25, 2012 at 23:17
2
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 :]
answered Aug 25, 2012 at 23:29
uınbɐɥsuınbɐɥs
7,0485 gold badges25 silver badges42 bronze badges
1
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.
answered Jul 25, 2016 at 12:11
George GGeorge G
7,12412 gold badges42 silver badges57 bronze badges
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']]] {
echo "Your error message!";
} else {
$test = $_GET['something'];
}
echo $test;
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'
answered Aug 25, 2012 at 23:21
sciritaisciritai
3,6381 gold badge16 silver badges22 bronze badges
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";
}
answered Aug 25, 2012 at 23:23
FThompsonFThompson
27.9k11 gold badges55 silver badges91 bronze badges
answered Aug 25, 2012 at 23:21
? is called Ternary [conditional] operator : example
answered Aug 25, 2012 at 23:21
Nikola NinkovicNikola Ninkovic
1,2521 gold badge12 silver badges26 bronze badges
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 = "";
}
answered Aug 25, 2012 at 23:26
bmorenatebmorenate
9551 gold badge8 silver badges18 bronze badges
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 overstrval[]
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:
- It fits neatly on one line and is clear what's going on.
- It's readable than
$value = isset[$_GET['something']] ? $_GET['something'] : '';
- Lower risk of copy/paste mistake or a typo:
$value=isset[$_GET['something']]?$_GET['somthing']:'';
- It's compatible with older and newer php.
Update Strict mode may require something like this:
$str_value = strval[@$_GET['something']];
$trimmed_value = trim[@$_GET['something']];
$int_value = intval[@$_GET['somenumber']];
answered Apr 1, 2017 at 4:28
TrophyGeekTrophyGeek
5,6542 gold badges34 silver badges31 bronze badges