Search code examples
phpfunctiontheorempythagorean

Function return: PHP


I'm not sure what I'm missing, but I can't get $c to output correctly.

<?php
    function pythThm($a, $b){
        $a2 = pow($a, 2);
        $b2 = pow($b, 2);
        $c = sqrt($a2 + $b2);
        if(is_int($c)){
            return $c;
        }
    }

    echo pythThm(3, 4);
    // Outputs nothing. it should be 5
?>

Solution to problem:

<?php
    function pythThm($a, $b){
        $a2 = pow($a, 2);
        $b2 = pow($b, 2);
        $c = sqrt($a2 + $b2);
        if($c - round($c) == 0){
            return $c;
        }
    }

    echo pythThm(4, 4);
    // Returns nothing
    echo pythThm(3,4);
    // Returns 5
?>

Solution

  • sqrt returns a float, which is not an int, so your function returns nothing. Just leave off the is_int check?