ternary operator & multiple conditions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
taldos
Forum Commoner
Posts: 39
Joined: Mon Aug 23, 2004 8:47 am
Location: Philadelphia

ternary operator & multiple conditions

Post 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
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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);
taldos
Forum Commoner
Posts: 39
Joined: Mon Aug 23, 2004 8:47 am
Location: Philadelphia

Post 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
Post Reply