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

NikiC
  • 100,734
  • 37
  • 191
  • 225

6 Answers6

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.

Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • 5
    More commonly/also known as the *ternary operator* – Justin Johnson Dec 30 '09 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 '09 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 '09 at 12:26
  • See also http://stackoverflow.com/questions/1976025/the-code-in-php/1976169#1976169 – Gumbo Dec 30 '09 at 12:29
  • @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 '09 at 12:33
  • @Gordon: Never mind. I just don’t want to call it ternary operator although that’s the common term for this operator. But I’m rather useing a less-known term than a in my opinion wrong term. – Gumbo Dec 30 '09 at 12:53
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'];
}
Filip Jukić
  • 1,174
  • 7
  • 11
  • 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 '09 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 '11 at 04:14
2
$pic_action=(isset($_POST['pic_action']))?($_POST['pic_action']):0;
ahmetunal
  • 3,930
  • 1
  • 23
  • 26
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;
}
Geert
  • 1,804
  • 15
  • 15
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
}
TRiG
  • 10,148
  • 7
  • 57
  • 107
0

You can do:

$_POST['pic_action'] = isset($_POST['pic_action']) ? $_POST['pic_action'] : 0;
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
  • 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 '09 at 12:30
  • @Alix Axel: And you don’t want to change your answer? – Gumbo Dec 30 '09 at 19:55