Jump to content

Nesting a ternary..


rwwd
 Share

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

>>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'.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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');

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.