Jump to content

set variable and compare in one command


NotionCommotion

Recommended Posts

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");}
Link to comment
Share on other sites

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.

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.