1. Pythagoras’ Theorem PHP Function

    April 12, 2008 by Callum Haywood

    I’ve been not so busy working on a really quick function for PHP that does pythagoras’ theorem. It works on the sum B2+C2=A2, but this can easily be changed, its just the way I was taught.

    Heres the code:

    <?php

    // PYTHAGORAS’ THEOREM FUNCTION FOR PHP
    // by Callum Haywood [callum@network467.co.uk]
    // Copyright 2008. All rights reserved.

    // This works on the calculation B² + C² = A², which
    // is what I’ve always been taught. Of course its
    // possible to change this, and its easy too.

    function pythagoras($b, $c)
    {
    $b1 = $b*$b; // Square value B
    $c1 = $c*$c; // Square value C
    $a1 = $b1+$c1; // Add them together
    $a2 = sqrt($a1); // Find the square root
    $a = round($a2, 1); // Round it to 1 DP

    echo “Side <b>B</b> is <b>$b</b>, and side <b>C</b> is <b>$c</b>. The computer has worked out that side <b>A</b> is <b>$a</b>!”;
    }

    pythagoras(“1″, “2″);

    ?>

    See, it’s simple, just change where it says 1 to the length of side B of your triangle, and where it says 2 to the length of side C of your triangle. Remeber that Pythagoras’ Theorem only works for right-angled triangles. You don’t have to be in KS3, but there is some more info here, that I stumbled across while revising.

    Hope someone finds this useful!


  2. teen4m and PHP

    January 23, 2008 by Callum Haywood

    Well I now know that teen4m only likes to work with PHP5. That’s good though, since support for PHP4 has been ceased.

    PHP4 just returns session_start(); errors. I know this because I’ve tested it on two servers with PHP4 installed on them.

    My server has PHP5 installed by default – I’ve used PHP5 since I set my server up. Hopefully PHP6 can bring some enhancements.

    Just a quick post this was…