36

I've been trying to use isset() in nested form like below:

isset($_POST['selectedTemplate'])?$_POST['selectedTemplate']:isset($_GET['selectedTemplate'])?$_GET['selectedTemplate']:0

But seems I'm missing something. Can anyone assist me how to do it?

3
  • 17
    Please don't use one-liners for that. It's unreadable. Commented Jan 4, 2012 at 22:56
  • 3
    Nesting the ternary operator really isn't recommended because it is completely non-intuitive: if you need to nest, you really should use full if/else syntax because it's much easier to read both for yourself and others Commented Jan 4, 2012 at 22:58
  • 1
    This bears the question why your form can be submitted via POST and GET in the first place. HTTP verbs have certain semantics to them.. Commented Jan 4, 2012 at 23:13

7 Answers 7

71

Wrap it in parentheses:

$selectedTemplate = isset($_POST['selectedTemplate'])
                  ? $_POST['selectedTemplate']
                  : (
                       isset($_GET['selectedTemplate'])
                       ? $_GET['selectedTemplate']
                       : 0
                  );

Or even better, use a proper if/else statement (for maintainability):

$selectTemplate = 0;

if (isset($_POST['selectedTemplate'])) {
    $selectTemplate = $_POST['selectedTemplate'];
} elseif (isset($_GET['selectedTemplate'])) {
    $selectTemplate = $_GET['selectedTemplate'];
}

However, as others have pointed out: it would simply be easier for you to use $_REQUEST:

$selectedTemplate = isset($_REQUEST['selectedTemplate'])
                  ? $_REQUEST['selectedTemplate']
                  : 0;
Sign up to request clarification or add additional context in comments.

6 Comments

Just a note: $_REQUEST takes more than $_POST and $_GET and the order can vary.
Cheers. Exactly what I needed, I did not know you had to wrap the whole second part of the statement in parenthesis.
Good on the maintainability.
C/C++ don't need parens for nested ?:, stackoverflow.com/q/18237432/2097284, so someone who comes to PHP from there gets a surprise.
@CamilleGoudeseune - C/C++ uses right-associativity by default for ternary expressions, while PHP uses left-associativity by default. In reality, left-associativity is almost never what you want, so PHP's default doesn't really make much sense. In fact, this is being deprecated in PHP 7.4.
|
10

As of PHP 7 we can use Null coalescing operator

$selectedTemplate = $_POST['selectedTemplate'] ?? $_GET['selectedTemplate'] ?? 0;

Comments

5

A little investigate here, and I guess, I've found real answer :)

Example code:

<?php

$test = array();
$test['a'] = "value";


var_dump(
    isset($test['a'])
        ? $test['a']
        : isset($test['b'])
            ? $test['b']
            : "default"
);

Be attentive to round brackets, that I've put.

I guess, you're waiting to get similar behavior:

var_dump(
    isset($test['a'])
        ? $test['a']
        : (isset($test['b'])  // <--  here
            ? $test['b']
            : "default")      // <--  and here
);

But! Real behavior looks like this:

var_dump(
    (isset($test['a'])        // <--  here
        ? $test['a']
        : isset($test['b']))  // <--  and here
            ? $test['b']
            : "default"
);

General mistake was, that you've missed notice: Undefined index.

Online shell.

Comments

4

It's simpler to read if we write ternary in the following manner:

$myvar = ($x == $y)
  ?(($x == $z)?'both':'foo')
  :(($x == $z)?'bar':'none');

But ternary operators are short, effective ways to write simple if statements. They are not built for nesting. :)

Comments

3

You might have an easier time simply using the $_REQUEST variables:

"$_REQUEST is an associative array that by default contains the contents of $_GET, $_POST and $_COOKIE."

https://www.php.net/manual/en/reserved.variables.request.php

Comments

2

I believe this will work:

$test = array();
$test['a'] = 123;
$test['b'] = NULL;


$var = (isset($test['a']) ? $test['a'] : (!isnull($test['b']) ? $test['b'] : "default"));

echo $var;

Comments

1

Instead of the ternary with the ambiguous precedence, you could just use $_REQUEST instead of the fiddly $_GET and $_POST probing:

 isset($_REQUEST['selectedTemplate']) ? $_REQUEST['selectedTemplate'] : 0

This is precisely what it is for.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.