views:

189

answers:

6

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

+19  A: 

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
More commonly/also known as the *ternary operator*
Justin Johnson
@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
@Gordon: "Ternary" just describes the number of arguments it involves (3), just as binary operators (`+`, `-`, `/`, etc) involve 2 arguments.
nickf
See also http://stackoverflow.com/questions/1976025/the-code-in-php/1976169#1976169
Gumbo
@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
@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
+2  A: 
$pic_action=(isset($_POST['pic_action']))?($_POST['pic_action']):0;
marvin
All those parenthesis are superfluous.
Justin Johnson
appropriate spacing *isn't* superfluous though...
nickf
@Justin Johnson: I prefer `($pic_action=((isset($_POST['pic_action']))?($_POST['pic_action']):(0)));` ;-)
Gumbo
A: 

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
+7  A: 

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'];
}
bezidejni
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
A: 

You can do:

$_POST['pic_action'] = isset($_POST['pic_action']) ? $_POST['pic_action'] : 0;
Alix Axel
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
@nickf: Yup, you're absolutely right.
Alix Axel
@Alix Axel: And you don’t want to change your answer?
Gumbo
+1  A: 
$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