isset PHP isset($_GET['something']) ? $_GET['something'] : ''
Asked Answered
C

8

41

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?

Caponize answered 25/8, 2012 at 23:17 Comment(2)
related #3737639Beadle
possible duplicate of What is the PHP ? : operator called and what does it do?Bunghole
A
90

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 :)
Acetone answered 25/8, 2012 at 23:29 Comment(1)
This isn't really plain.Patentee
V
17

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.

Voccola answered 25/7, 2016 at 12:11 Comment(0)
T
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'
Thermodynamic answered 25/8, 2012 at 23:21 Comment(2)
The code blocks in the if-else conditional statement are swapped: the error should be output in case the parameter is missing.L
fix code - if (!isset($_GET['something'])) - put ! befor issetAgreement
A
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";
}
Anemo answered 25/8, 2012 at 23:23 Comment(0)
N
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

Nicolenicolea answered 25/8, 2012 at 23:21 Comment(0)
R
1

? is called Ternary (conditional) operator : example

Rastus answered 25/8, 2012 at 23:21 Comment(0)
D
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 = "";
}
Del answered 25/8, 2012 at 23:26 Comment(0)
M
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']);
Malaguena answered 1/4, 2017 at 4:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.