Tricky question in practice PHP Zend Certification exam

I am currently studying towards my Zend PHP certification and have came across this question in my first practice exam:

What does the following function do, when passed two integer values for $p and $q?

The available answers are:
        Loops infinitely
      Switches the values of $p and $q
      Determines if they are both even or odd
      Determines the greatest common divisor between them

Any help appreciated,
Picco
      Calculates the modulus between the two
<?php
function magic($p, $q) {
  return ($q == 0)
    ? $p
    : magic($q, $p % $q);
}
?>

Open in new window

crmpiccoAsked:
Who is Participating?
 
hieloCommented:
It Determines the greatest common divisor between them. Trace it manually:
<?php
function magic($p, $q) {
  return ($q == 0)
    ? $p
    : magic($q, $p % $q);
}

magic(4,5);
/*
	magic(5,4);
		magic(4,1);
			magic(1,0);
				1


*/
magic(2,4)
/*
	magic(4,2);
		magic(2,0);
			2
*/
?>

Open in new window

0
 
HeiniHogCommented:
Determines the GCD between them.
0
 
rajkumar_pbCommented:
Answer is :

Determines the greatest common divisor between them

as said by others. you need to trace it manually.
0
 
crmpiccoAuthor Commented:
excellent!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.