Jump to content
Killersites Forums

ternary operator


fahim
 Share

Recommended Posts

Hi someone asks me the output of the following code when two integer values pass for $m and $n:

<?php

function run($m, $n)

{

return ($n == 0) ? $m: run($n, $m % $n);

}

?>

 

I am confused about this . So please explain the output for better understanding.

Thanks.

Edited by fahim
Link to comment
Share on other sites

The syntax

return ($n == 0) ? $m: run($n, $m % $n);

means... If $n is 0, return $m; If not, return run($n, $m % $n) and is identical in functionality to this:

if ($n==0)
 return $m;
else
 return run($n, $m % $n);

 

 

So if $n is 0 the function runs and spits out $m.

 

If $n is not 0 the function will run AGAIN but this time using the remainder of $m / $n for argument $n..

Edited by khanahk
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
 Share

×
×
  • Create New...