Forum Moderators: coopster

Message Too Old, No Replies

nested ternary issue

getting the right conditional string output

         

Matthew1980

8:09 pm on Jan 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there people of webmaster world,

Could anyone give me a pointer with the following code please:-

$login_check = ((!isset($_SESSION['admin']) && isset($_COOKIE['Login'])) ? "Cookie yes but no session" :
(isset($_SESSION['admin']) && !isset($_COOKIE['Login'])) ? "session but no cookie" :
(isset($_SESSION['admin']) && isset($_COOKIE['Login'])) ? "Session and cookie": "Nothing is set");

The strings in there are just for debugging..

I'm not sure whether this is the best way to go about this, but depending on how & if Cookie or Session is set depends on what sql statement is issued. This is instead of having multiple if clauses.

Thanks for any help.

MRb

TheMadScientist

6:52 am on Jan 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If I remember correctly, you can only use 'shorthand' for simple statements, meaning ? = if left of the : = true right of the : = false, but you cannot have multiple statements like you do... The short answer is I think you're going to have to write it out. (I almost always do.)

dreamcatcher

7:52 am on Jan 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, Matthew1980 is correct, you can have nested ternary statements. From what I can see, this seems to be all you need:

$login_check = (isset($_SESSION['admin']) ¦¦ isset($_COOKIE['Login']) ? (isset($_SESSION['admin']) ? 'session but no cookie' : 'Session and cookie') : 'Nothing is Set');

dc

TheMadScientist

11:55 pm on Jan 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Cool thanks... I haven't really used them too often, because I thought they only worked for simple statements... I might have to start!