Jump to content

[SOLVED] explain "return == ? : ;" please


Ryokotsusai

Recommended Posts

I have an old somewhat broken script that I am trying to get working again, and there is a weird statement that comes up quite a bit

$something['this'] == 'abc' ? 'def' : 'ghi';

 

I can't for the life of me figure out what it is trying to do  :(

 

any help would be greatly appreciated  ;)

 

Link to comment
Share on other sites

wow, there is a lot out there, i thought ==, != and all those were only for use in an if/while/etc

 

there is a lot of stuff i still need to learn  :P

 

EDIT>>

 

actually that has confused me too, so forgive for being a little slow with this, but what exactly is this evaluating for the second statement:

return $settings['ses_type'] == 'adv' ? 'ses' : 'tname'

Link to comment
Share on other sites

If...

 

return $settings['ses_type'] == 'adv' ? 'ses' : 'tname';

 

is the actual code, it is exactly the same as...

 

<?php

 if ($settings['ses_type'] == 'ses') {
   return true;
 } else {
   return false;
 }

?>

 

To be honest though... that snippet makes little sense. maybe this is part of the reason your somewhat broken script is somewhat broken.

 

This part...

 

'adv' ? 'ses' : 'tname';

 

Is a ternary operator that checks if the first expression is true, if so it returns the second expression, else it returns the third. The expression 'adv' will always be true, so this ternary operator will always return 'ses'. having te first expression hard coded like that simply makes no sense.

Link to comment
Share on other sites

just to correct thorpe a little, the equivalent of this:

 

return $settings['ses_type'] == 'adv' ? 'ses' : 'tname';

 

is this:

 

if ($settings['ses_type'] == 'adv')
{
  return 'ses';
}
else
{
  return 'tname';
}

 

it uses the portion before the question mark as the condition, returning (since the line begins with the return command) 'ses' if true, 'tname' if false.

Link to comment
Share on other sites

if it returns, tname, there is nothing that can use that in the situation it comes up in, so i have no idea what was going on anyway

+

if it returns ses it needs tname  ??? so i got nothing on this

 

but now at least i understand what is going on when i see == ? : ; come up again

Link to comment
Share on other sites

Personally I suggest against writing code that looks like that. It can save you a few lines of code but the meaning as to what you're trying to accomplish is much more clear the other way... and as you're finding out when you go back to a script it's hard to figure out what you were doing months ago when you last worked on it. I typically try to always stay on the side of readability.

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.