How to use the ternary here?

-1
$rs = $pbc->selectPublication(filter_input(INPUT_GET, 'id', FILTER_DEFAULT));

I have this code that is passed as method argument to select the posts in the system, but how can I use it with ternary, if it returns true and is filtered without problems pass the value, or if it gives any problem, send 0?

My select class:

$selectSQL = $this->pdo->prepare('SELECT values WHERE id = ? LIMIT 1');

            $selectSQL->bindValue(1, $codigo, PDO::PARAM_INT);
            $selectSQL->execute();
            $row = $selectSQL->fetch(PDO::FETCH_ASSOC);

            return $row;
    
asked by anonymous 29.09.2017 / 21:43

1 answer

1
$rs = $pbc->selectPublication(filter_input(INPUT_GET, 'id', FILTER_DEFAULT));

echo ($rs) ? $rs : '0';

If the variable has a return, then either it has "content" or is 0 or false correct?! With this:

$rs = true;
echo ($rs) ? $rs : '0';
$rs = false;
echo ($rs) ? $rs : '0';
$rs = 'okok';
echo ($rs) ? $rs : '0';
// pegadinha
$rs = 'false';
echo ($rs) ? $rs : '0';
    
29.09.2017 / 21:46