Jump to content

Ternary Operator ( ? : )


timothyarden
Go to solution Solved by timothyarden,

Recommended Posts

Hi,
Just wanted to check with everyone that I am getting the Logic right with the use of the Ternary ? : Operator

isset( $Files ) ? isset( $Directories ) ? __DisplayResults__( $Files, $Directories ) : __DisplayResults__( $Files, NULL ) : isset( $Directories ) ? __DisplayResults__( NULL, $Directories ) : $error = "Neither Directories or Folders are set";

So that should do the same as

            if( isset( $Files ) && isset( $Directories ) ){
                __DisplayResults__( $Files, $Directories );
            } elseif( isset( $Files ) ){
                __DisplayResults__( $Files, NULL );
            } elseif( isset( $Directories ) ){
                __DisplayResults__( NULL, $Directories );
            } else {
                 $error = "Neither Directories or Folders are set"
            }

Thanks :)

Edited by timothyarden
Link to comment
Share on other sites

  • Solution

Nevermind, managed to figure it out on my own, needed Parenthesis

Example:
$Files = TRUE;
$Directories = NULL;
    
isset( $Files ) ? ( isset( $Directories ) ? $a = "Files & Directories" : $a = "Files" ) : ( isset( $Directories ) ? $a = "Directories" : $a = "Neither" );
    
echo $a;
Link to comment
Share on other sites

The problem with what you've done is that it makes the code much harder to read. When you come back to this in the future, or if somebody else has to work with your code, your nested ternary operations require much more effort to review. If instead you did something like this, anyone can understand what is going on in about 1 second:

$f = isset( $Files ) ? $Files : NULL;
$d =  isset( $Directories ) ? $Directories : NULL;

if( is_null($f) && is_null($d) ){
	$error = "Neither Directories or Folders are set";
}else{
	__DisplayResults__( $f, $d );
}
Link to comment
Share on other sites

Yeah, I wanted a one line solution though haha
the parenthesis work fine and make it easily readable

also unless Im not reading your code right it would need 2 more elseif conditions - I just found it much simpler to it in the one line instead of 10

thanks for the tip though :)

Edited by timothyarden
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

×
×
  • 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.