Take the 2-minute tour ×

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?

share|improve this question
related stackoverflow.com/questions/3737139/… –  Musa Aug 25 '12 at 23:21
possible duplicate of What is the PHP ? : operator called and what does it do? –  Niko Aug 25 '12 at 23:22

6 Answers 6

up vote 19 down vote accepted

It's commonly referred to as 'shorthand' or the Ternary Operator.

$test = isset($_GET['something']) ? $_GET['something'] : '';


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)


// test --v
if(isset(...)) { // if test is true, do ... (equivalent to ?)
    $test = // assign variable
} else { // otherwise... (equivalent to :)
share|improve this answer

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


is equivalent to

} 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'
share|improve this answer

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.


$something = isset($_GET['something']) ? $_GET['something'] : "failed";


    $something = $_GET['something'];
} else {
    $something = "failed";
share|improve this answer

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

share|improve this answer

? is called Ternary (conditional) operator : example

share|improve this answer

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 = "";
share|improve this answer

Your Answer


