Ryokotsusai Posted August 23, 2007 Share Posted August 23, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/66414-solved-explain-return-please/ Share on other sites More sharing options...
nathanmaxsonadil Posted August 23, 2007 Share Posted August 23, 2007 <?php $first ? $second : $third ?> If the value of the first subexpression is TRUE (non-zero), then the second subexpression is evaluated, and that is the result of the conditional expression. Otherwise, the third subexpression is evaluated, and that is the value. from http://us.php.net/manual/en/language.expressions.php Quote Link to comment https://forums.phpfreaks.com/topic/66414-solved-explain-return-please/#findComment-332425 Share on other sites More sharing options...
Ryokotsusai Posted August 23, 2007 Author Share Posted August 23, 2007 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 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' Quote Link to comment https://forums.phpfreaks.com/topic/66414-solved-explain-return-please/#findComment-332436 Share on other sites More sharing options...
trq Posted August 23, 2007 Share Posted August 23, 2007 The == (comparison operator) won't work in the snippet you provided. It needs to be = (assignment operator). Quote Link to comment https://forums.phpfreaks.com/topic/66414-solved-explain-return-please/#findComment-332440 Share on other sites More sharing options...
nathanmaxsonadil Posted August 23, 2007 Share Posted August 23, 2007 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' What I understand is '1st' ? '2nd' : '3rd' Quote Link to comment https://forums.phpfreaks.com/topic/66414-solved-explain-return-please/#findComment-332443 Share on other sites More sharing options...
Ryokotsusai Posted August 24, 2007 Author Share Posted August 24, 2007 i guess my question about that more specifically is what is it 'evaluating' when it hits 'ses' ? is it just reading it, or is it doing something there Quote Link to comment https://forums.phpfreaks.com/topic/66414-solved-explain-return-please/#findComment-332457 Share on other sites More sharing options...
trq Posted August 24, 2007 Share Posted August 24, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/66414-solved-explain-return-please/#findComment-332460 Share on other sites More sharing options...
Ryokotsusai Posted August 24, 2007 Author Share Posted August 24, 2007 Thank you, I was able to get it working by commenting that out and having it return what i wanted it to, it is only a temporary fix until i figure out what was being attempted there though... Quote Link to comment https://forums.phpfreaks.com/topic/66414-solved-explain-return-please/#findComment-332473 Share on other sites More sharing options...
akitchin Posted August 24, 2007 Share Posted August 24, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/66414-solved-explain-return-please/#findComment-332476 Share on other sites More sharing options...
trq Posted August 24, 2007 Share Posted August 24, 2007 I guess I misread the code. I would write that... return ($settings['ses_type'] == 'adv') ? 'ses' : 'tname'; but your right akitchen. Ignore my posts all together, I just read the code wrong. Quote Link to comment https://forums.phpfreaks.com/topic/66414-solved-explain-return-please/#findComment-332489 Share on other sites More sharing options...
akitchin Posted August 24, 2007 Share Posted August 24, 2007 i know, it drives me insane when people don't place their conditions within parentheses. Quote Link to comment https://forums.phpfreaks.com/topic/66414-solved-explain-return-please/#findComment-332493 Share on other sites More sharing options...
Ryokotsusai Posted August 24, 2007 Author Share Posted August 24, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/66414-solved-explain-return-please/#findComment-332512 Share on other sites More sharing options...
teng84 Posted August 24, 2007 Share Posted August 24, 2007 maybe $teng = ($ok=='no')?'cool':'no'; echo $teng; //give you something like cool //is that what you mean Quote Link to comment https://forums.phpfreaks.com/topic/66414-solved-explain-return-please/#findComment-332514 Share on other sites More sharing options...
trq Posted August 24, 2007 Share Posted August 24, 2007 but now at least i understand what is going on when i see == ? : ; come up again I dont think you do as its not == ? : ; but <expression> ? <expression> : <expression>; See the manual entry here. Quote Link to comment https://forums.phpfreaks.com/topic/66414-solved-explain-return-please/#findComment-332519 Share on other sites More sharing options...
dbo Posted August 24, 2007 Share Posted August 24, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/66414-solved-explain-return-please/#findComment-332568 Share on other sites More sharing options...
Ryokotsusai Posted August 24, 2007 Author Share Posted August 24, 2007 as its not == ? : ; but <expression> ? <expression> : <expression>; i know, but i am a tad lazy and typing the whole thing out goes against laziness Quote Link to comment https://forums.phpfreaks.com/topic/66414-solved-explain-return-please/#findComment-332682 Share on other sites More sharing options...
trq Posted August 24, 2007 Share Posted August 24, 2007 My point is, the == (comparison operator) has nothing at all to do with the ternary operator. Quote Link to comment https://forums.phpfreaks.com/topic/66414-solved-explain-return-please/#findComment-332702 Share on other sites More sharing options...
Ryokotsusai Posted August 24, 2007 Author Share Posted August 24, 2007 your right, i didn't :'( u could have $this ? if($me == $notme) {something} : $that; ? and how does it evaluate something like 'ses' Quote Link to comment https://forums.phpfreaks.com/topic/66414-solved-explain-return-please/#findComment-332838 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.