Welcome Guest, Not a member yet? Register   Sign In
Ternary operator, please help me fix this.
#1

[eluser]whygod[/eluser]
Hi guys.

I have this ternary operator.
Could you guys help me fix this into right syntax.
Code:
($q->num_rows() > 0) ? return TRUE : return FALSE;

Currently this is not working.

Thanks in advanced.
#2

[eluser]PhilTem[/eluser]
You can just use

Code:
return $q->num_rows() > 0;

there's not even the necessity to return TRUE or FALSE explicitly since the return value is already boolean.
#3

[eluser]TunaMaxx[/eluser]
@PhilTem's answer is right way to do it.

However, for your reference, the problem with your code is that you're trying to return based on the result of the condition. You need to return, and then alter the value based on the condition, like so:

Code:
return ($q->num_rows() > 0) ? TRUE : FALSE;

But don't do it this way, use @PhilTem's example code!
#4

[eluser]Aken[/eluser]
If you're like me, you might find doing this "easier". It helps remind me that I'm returning the conditional value, so I can skim and debug code quicker. It functions the same. Although in some cases, for more complicated conditionals, the parentheses may be required.

Code:
return ($q->num_rows() > 0);




Theme © iAndrew 2016 - Forum software by © MyBB