Jump to content
Killersites Community
Sign in to follow this  
fahim

ternary operator

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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×