Jump to content
Killersites Forums

ternary operator


Recommended Posts

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


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.


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;
 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.

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.


  • Create New...