KKhanye
KKhanye

Reputation: 117

If statement return wrong value

I have a strange problem, a statement works correct on a test server but when I upload it to a live server I get an error.

My problem is as follows, I return a true or false depending on the number of rows of a query, I have tested the query and seem to have a problem

$row = mysqli_num_rows($result);
//$row for this example equals zero (0)
return ($row > 0) ? false : true;

It returns true, I am thinking maybe I should use an if statement, but I want to find out why this is returning true and it should return false;

Thank you in advance

Upvotes: 0

Views: 146

Answers (2)

TunaMaxx
TunaMaxx

Reputation: 1769

You don't need the ternary. just do this:

return ($row > 0);

You're already getting the Boolean from the result. No need to complicate it.

Upvotes: 1

BenM
BenM

Reputation: 53218

I don't entirely understand your question, but I'm going to take a shot in the dark here and say that you've got the return values the wrong way around in your ternary operator.

Try this:

return ($row > 0) ? true : false;

You can improve this even more so by doing:

return ($row > 0);

For reference, a ternary operator is the equivalent of writing:

if($row > 0)
{
    return true;
}
else
{
    return false;
}

So there's no need to rewrite it to an if() statement.

Upvotes: 6

Related Questions