fahim Posted August 23, 2012 Report Share Posted August 23, 2012 (edited) 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 August 23, 2012 by fahim Quote Link to comment Share on other sites More sharing options...
falkencreative Posted August 23, 2012 Report Share Posted August 23, 2012 See http://www.johnhok.com/2008/02/23/php-tip-tertiary-operator/ http://php.net/manual/en/language.operators.comparison.php (example #2) Quote Link to comment Share on other sites More sharing options...
fahim Posted August 23, 2012 Author Report Share Posted August 23, 2012 Still in confusing. Please would let me know something about the output of the code? Quote Link to comment Share on other sites More sharing options...
khanahk Posted August 23, 2012 Report Share Posted August 23, 2012 (edited) 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 August 24, 2012 by khanahk Quote Link to comment 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.
Note: Your post will require moderator approval before it will be visible.