0

Is it possible to rewrite this to be shorter somehow?

if (isset($_POST['pic_action'])){
  $pic_action=$_POST['pic_action'];
}
else { 
  $pic_action=0;
}

I have seen it somewhere but forgot... :/

BTW, please explain your code also if you like!

Thanks

6 Answers 6

18

You could use the conditional operator ?::

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

The conditional operator expression expr1 ? expr2 : expr3 evaluates to the return value of expr2 if the evaluated return value of expr1 is true; otherwise the expression evaluates to the evaluated return value of expr3. So if isset($_POST['pic_action']) evaluates to true, the whole expression evaluates to the evaluated value of $_POST['pic_action'] and to the evaluated value of 0 otherwise.

So in short: if isset($_POST['pic_action']) is true, $pic_action will hold the value of $_POST['pic_action'] and 0 otherwise.

6
  • 5
    More commonly/also known as the ternary operator Dec 30, 2009 at 11:51
  • 2
    @Justin Johnson: But to be correct it’s just a ternary operator and not the ternary operator. (Although there doesn’t come any other ternary operator in my mind right now.)
    – Gumbo
    Dec 30, 2009 at 12:20
  • 1
    @Gordon: "Ternary" just describes the number of arguments it involves (3), just as binary operators (+, -, /, etc) involve 2 arguments.
    – nickf
    Dec 30, 2009 at 12:26
  • @nickf well, it says Ternary operator in the manual, which is of course, also a conditional operator. But I'd guess most PHP people will find it easier to know what is meant by the first. @Gumbo Sorry. It's your post. Name it it any way you like.
    – Gordon
    Dec 30, 2009 at 12:33
8

Gumbo's answer is probably the best way.

It can also be written as:

$pic_action = 0;
if (isset($_POST['pic_action'])){
    $pic_action=$_POST['pic_action'];
}
2
  • 2
    I usually use this one over the ternary operator, because I find it easier to understand what's going on or when there is some more logic to do in the IF block
    – Gordon
    Dec 30, 2009 at 12:26
  • I avoid the ternary operator and use this method so that my test coverage is more useful. While specific values can make certain code fail it is more likely that a missed branch (being the calculation of the value within the if or ternary) will lead to failures. For the magic 100% code coverage only half of all ternary branches may still be checked.
    – Paul
    May 17, 2011 at 4:14
2
$pic_action=(isset($_POST['pic_action']))?($_POST['pic_action']):0;
2
  • 2
    All those parenthesis are superfluous. Dec 30, 2009 at 11:51
  • @Justin Johnson: I prefer ($pic_action=((isset($_POST['pic_action']))?($_POST['pic_action']):(0))); ;-)
    – Gumbo
    Dec 30, 2009 at 12:28
1
$pic_action = array_get($_POST, 'pic_action', 0);

The line above requires the array_get function defined below. Source from Kohana's Arr class. Very small and generic function. Can be used on all arrays, e.g. $_GET.

/**
 * Retrieve a single key from an array. If the key does not exist in the
 * array, the default value will be returned instead.
 *
 * @param   array   array to extract from
 * @param   string  key name
 * @param   mixed   default value
 * @return  mixed
 */
function array_get(array $array, $key, $default = NULL)
{
    return isset($array[$key]) ? $array[$key] : $default;
}
0

Longer, but reusable:

$pic_action = QueryPost('pic_action', 0);

function QueryPost($name, $default='', $valid=false) {
    if (!isset($_POST[$name])) return $default;
    if (($valid) and (empty($_POST[$name]))) return $default;
    return $_POST[$name];
}

Or you could have the QueryPost function do a form of validation while you're at it.

$pic_action = QueryPost('pic_action', 'int', 0);

function QueryPost($name, $rule, $default='', $valid=false) {
    // this shouldn't be too hard to write
}
0

You can do:

$_POST['pic_action'] = isset($_POST['pic_action']) ? $_POST['pic_action'] : 0;
2
  • 2
    No - this would set $_POST['pic_action'] to either true if it is set, or 0 if it is not set. $x = $y ?: $z is the equivalent of $x = $y ? $y : $z
    – nickf
    Dec 30, 2009 at 12:30
  • @Alix Axel: And you don’t want to change your answer?
    – Gumbo
    Dec 30, 2009 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.