Page 1 of 1

ternary operator & multiple conditions

Posted: Mon Dec 04, 2006 11:52 am
by taldos
Does the ternary handle multiple conditions? or is only for single contidional statements.

ie: I know this is legal

Code: Select all

$firstname = (isset($_POST['fname'])) ? $_POST['fname'] : -1;
do multiple conditions work?

Code: Select all

$firstname = (isset($_POST['fname']) && ereg("^[a-zA-Z]+$", $_POST['fname'])) ? $_POST['fname'] : -1;

Reason for my asking is that I'm lazy :-). I already have series of ternary statements with single conditions and I don't feel like changing them to full blown 'if' statements, but I will if ternary operators don't handle multiple conditions.

As always, laziness breeds innovations & new thinking :-D

Posted: Mon Dec 04, 2006 11:56 am
by Burrito
ok being lazy is fine...I am your king actually.

but if you're not so lazy that you can write it out in here, why can't you just test it?

to answer your question, you can use multiple conditions, but c'mon give it a try first!

Code: Select all

$someVar = ($someTrue && $someInt < 5 ? TRUE : FALSE);

Posted: Mon Dec 04, 2006 12:06 pm
by taldos
yup gave it a try. And just wanted to rule this out as a possibility before I switched over to 'if' statements. Now I know that the errors were caused by invalid code as opposed to doing something illegal with the operator.

thx :-D