find the nth root of a number in php

To find the nth root of a number in PHP, you can use the pow() function which raises a number to the power of another number. To find the nth root of a number, you can raise that number to the power of 1/n. Here's an example function that finds the nth root of a given number:

main.php
function nth_root($number, $n) {
    return pow($number, 1 / $n);
}
68 chars
4 lines

You can call this function by passing in the number you want to find the root of and the value of n:

main.php
echo nth_root(8, 3); // Output: 2 - The cube root of 8 is 2
echo nth_root(27, 3); // Output: 3 - The cube root of 27 is 3
122 chars
3 lines

Note that this function only works for finding the real nth roots of real numbers. If you need to find complex roots or want to handle edge cases such as negative numbers with even values of n, you'll need to modify the function accordingly.

gistlibby LogSnag