# Write a function to return the base to the power of its exponent?

Answered

First write the function and then suggest the PHP buit-in method for this.

Best answer

You must write it as a recursive function in order to get the base multiplied by the exponent recursively.

function power($num, $pow) { if ($pow < 1) return 1; else return $num * power($num, $pow-1); } //echo power(2, 3); //echo pow(2, 3); // This is the php built-in method //echo 2 ** 3; //This is beyond php 5.6