rwwd Posted October 14, 2010 Share Posted October 14, 2010 if($AClass->select_error || $BClass->ErrorFile){ include("/path/to/files/".($AClass->select_error ? 'no_selector' : 'error_file_missing').".php"); exit; } if($CClass->WriteError){ include("/path/to/files/FileWriteError.php"); exit; } Probably a simple one for you folks, but I can't for the life of me work out how to make the above into 1 line of ternarys. Cheers, Rw Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted October 14, 2010 Share Posted October 14, 2010 You can't because you have two separate ifs that must run, if the second if was actually an elseif then it might be doable. Quote Link to comment Share on other sites More sharing options...
gizmola Posted October 14, 2010 Share Posted October 14, 2010 Using the ternary is an alternative to a simple if - then -else. If it's not an if - then - else, then it's not going to be translatable to a ternary. Quote Link to comment Share on other sites More sharing options...
rwwd Posted October 14, 2010 Author Share Posted October 14, 2010 >>if the second if was actually an elseif then it might be doable Technically it is an else if, I just wrote them separately as I am tweaking a system I wrote last year.. Basically the if evaluates like this:- if((condition1 == true) || (condition2 == true) || (condition3 == true)) then include the file depending on which clause evaluated true... I shall sleep on it, your probably right though, as it stands it works, but I just wanted to shave a few lines from the main parsing file.. Rw Quote Link to comment Share on other sites More sharing options...
Oziam Posted October 14, 2010 Share Posted October 14, 2010 How would I put the following into a ternary? I need the result to be more than 1 argument. if($var == $str){ $a = 'foo'; $b = 'bar'; } else{ $a = 'bar'; $b = 'foo'; } can you concantenate the resulting arguments? Thanks! Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted October 14, 2010 Share Posted October 14, 2010 >>if the second if was actually an elseif then it might be doable Technically it is an else if, I just wrote them separately as I am tweaking a system I wrote last year.. Basically the if evaluates like this:- if((condition1 == true) || (condition2 == true) || (condition3 == true)) then include the file depending on which clause evaluated true... I shall sleep on it, your probably right though, as it stands it works, but I just wanted to shave a few lines from the main parsing file.. Rw I would never do this and I would absolutely hate to maintain code like this, but here you go (please don't do this): $AClass->select_error || $BClass->ErrorFile || $CClass->WriteError ? die(include("/path/to/files/".($AClass->select_error ? 'no_selector' : ($BClass->ErrorFile ? 'error_file_missing' : 'FileWriteError').".php"))) : false; The caveat is that if more than one of the 3 conditions are true then the first one that is true will decide the file that is included in this order: 'no_selector', error_file_missing' , 'FileWriteError'. Quote Link to comment Share on other sites More sharing options...
rwwd Posted October 14, 2010 Author Share Posted October 14, 2010 Hi there AbraCadaver, Well, thanks for your efforts anyway, but I shall heed your opinion when I try this tomorrow. The more I look at this the more I think, "What the hell, it's only a few extra lines of code", and as you say, not very easy to maintain.. Cheers anyway. Rw Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted October 14, 2010 Share Posted October 14, 2010 How would I put the following into a ternary? I need the result to be more than 1 argument. if($var == $str){ $a = 'foo'; $b = 'bar'; } else{ $a = 'bar'; $b = 'foo'; } can you concantenate the resulting arguments? Thanks! Just for fun (please leave the ternaries alone): extract($var == $str ? array('a'=>'foo','b'=>'bar') : array('a'=>'bar','b'=>'foo')); --or-- list($a, $b) = $var == $str ? array('foo','bar') : array('bar','foo'); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.