How to use the ternary here?

Asked

Viewed 55 times

-2

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

I have this code that is passed as argument of the method to select the posts on the system, but how can I use it with ternary, if it returns true and is filtered smoothly pass the value, or if 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;
  • What is his return, in case of error?

  • 0, or if there is a problem, send 0.

  • 3

    If his return, when he makes a mistake, is already zero, why do you need a ternary?

  • I didn’t know that returns 0

  • Ah, I thought you said zero.

  • But there’s a way you can stick the method selectPublication to facilitate the response?

Show 1 more comment

1 answer

0

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

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

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

$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';
  • How does this ($rs) ? $rs : '0'; work? Put in answer please.

  • This is how you comment on the answer ? I never used stack

  • Face as well gives -1 !? One can have several understandings of the question, as various results... by love...

  • 1

    Buddy, I didn’t give you -1

  • It was Felipe Avelar then... what a mess...

  • 2

    Certain comments that should be avoided, read here, will help you: https://answall.com/help/privileges/comment

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.