40

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?

user1625186
  • 403
  • 1
  • 4
  • 4
  • 2
    related http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php – Musa Aug 25 '12 at 23:21
  • 1
    possible duplicate of [What is the PHP ? : operator called and what does it do?](http://stackoverflow.com/questions/1080247/what-is-the-php-operator-called-and-what-does-it-do) – Niko Aug 25 '12 at 23:22

8 Answers8

89

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 :)
uınbɐɥs
  • 7,236
  • 5
  • 26
  • 42
16

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.

a stone arachnid
  • 1,272
  • 1
  • 15
  • 27
George G
  • 7,443
  • 12
  • 45
  • 59
8

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'
sciritai
  • 3,688
  • 1
  • 17
  • 22
  • The code blocks in the if-else conditional statement are swapped: the error should be output in case the parameter is missing. – Vitaliy May 11 '23 at 17:13
4

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";
}
FThompson
  • 28,352
  • 13
  • 60
  • 93
2

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

mathematician1975
  • 21,161
  • 6
  • 59
  • 101
1

? is called Ternary (conditional) operator : example

Nikola Ninkovic
  • 1,252
  • 1
  • 12
  • 27
1

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 = "";
}
bmorenate
  • 963
  • 1
  • 8
  • 18
1

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:

  • 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']);
TrophyGeek
  • 5,902
  • 2
  • 36
  • 33