NotionCommotion Posted January 9, 2015 Share Posted January 9, 2015 I typically only use the first two solutions. Is there anything wrong with the third? Is there any more streamlined way to do it? function f1($x){return $x*1;} function f2($x){return $x*1;} $x=2;$y=1; if(f1($x)>f2($y)){echo(f2($y)."\n");} // or $f2_y=f2($y); if(f1($x)>$f2_y){echo($f2_y."\n");} // or if(f1($x)>$f2_y=f2($y)){echo($f2_y."\n");} Quote Link to comment https://forums.phpfreaks.com/topic/293773-set-variable-and-compare-in-one-command/ Share on other sites More sharing options...
phpaddiction Posted January 9, 2015 Share Posted January 9, 2015 You could do it this way: echo ( f1($x) > ($f2_y = f2($y)) ? $f2_y ."\n" : ""; That should work. I put the $f2_y inside brackets to have that executed first and assign f2($x) to $f2_y, then it should run the comparison and echo on true. Quote Link to comment https://forums.phpfreaks.com/topic/293773-set-variable-and-compare-in-one-command/#findComment-1502268 Share on other sites More sharing options...
phpaddiction Posted January 9, 2015 Share Posted January 9, 2015 Sorry a syntax problem on my first post echo ( f1($x) > ($f2_y = f2($y))) ? $f2_y ."\n" : ""; This is correct Quote Link to comment https://forums.phpfreaks.com/topic/293773-set-variable-and-compare-in-one-command/#findComment-1502272 Share on other sites More sharing options...
NotionCommotion Posted January 9, 2015 Author Share Posted January 9, 2015 Thanks phpaddiction, and welcome to php freaks. Do you know whether the parenthesis around setting $f2_y are absolutely required. I looked at http://php.net/manual/en/language.operators.precedence.php, and wasn't sure whether = had higher precedence than >. Regardless, I feel they make the code more readable. Quote Link to comment https://forums.phpfreaks.com/topic/293773-set-variable-and-compare-in-one-command/#findComment-1502274 Share on other sites More sharing options...
Barand Posted January 9, 2015 Share Posted January 9, 2015 Do you know whether the parenthesis around setting $f2_y are absolutely required. You could always try it and see. Quote Link to comment https://forums.phpfreaks.com/topic/293773-set-variable-and-compare-in-one-command/#findComment-1502276 Share on other sites More sharing options...
NotionCommotion Posted January 9, 2015 Author Share Posted January 9, 2015 You could always try it and see. I had done so before starting this post. As far as I could tell, the equal sign has higher precedence than the > sign, and the parenthesis are not required. Just couldn't find this documented anywhere. Quote Link to comment https://forums.phpfreaks.com/topic/293773-set-variable-and-compare-in-one-command/#findComment-1502279 Share on other sites More sharing options...
kicken Posted January 9, 2015 Share Posted January 9, 2015 I had done so before starting this post. As far as I could tell, the equal sign has higher precedence than the > sign, and the parenthesis are not required. Just couldn't find this documented anywhere. Actually it has lower precedence.. Since the = comes after the > in the example, it works without the extra parenthesis. This on the other hand: if($f1_x=f1($x) > f2($y)){ would be interpreted as $f1_x = (f1($x) > f2($y)) and $f1_x would be a boolean true/false not the return value of the function. Quote Link to comment https://forums.phpfreaks.com/topic/293773-set-variable-and-compare-in-one-command/#findComment-1502322 Share on other sites More sharing options...
NotionCommotion Posted January 10, 2015 Author Share Posted January 10, 2015 Thanks Kicken, I am sure this is a little tiny detail which could cause a not so little headache. Quote Link to comment https://forums.phpfreaks.com/topic/293773-set-variable-and-compare-in-one-command/#findComment-1502355 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.