0

Okay, I've rubber-ducked this enough, time to ask for help.

I'm new to PHP, I'm trying to do some math with values provided in an HTML form and one of my functions isn't working. The funny part is another function written in basically identical way is working just fine.

Here's my PHP code:

//For passing values around
    $area = 50;
    $plants = 0;
    $pi = 3.14;

    //Calculate the area
    function calcsquare($len, $wid) {
        $a = (double) $len * (double) $wid;
        return $a;
    }

    function calccirc($rad, $pi) {
        $a = (double) $rad * (double) $pi;
        return $a;
    }

    //Make the buttons do stuff
    if($_SERVER['REQUEST_METHOD'] == "POST" and isset($_POST['calcarea_button']))
    {
        if($POST["radius"] == "") {
            $area = calcsquare((double) $_POST["length"], (double) $_POST["width"]);
        } else {
            //$area = calccirc((double) $_POST["radius"], (double) $pi);
            //$area = $_POST["radius"];
            //$area = 3.14;
            $area = "Seriously wtf.";
        }
    }

(I know I haven't added any error handling. That is a problem for Future Me.)

And here's the relevant HTML:

<div class="calcarea">
    <!--Calculate the area-->
    <form action="calc_plants.php" method="post">
        Length: <input name="length" type="text" /> ft.<br>
        Width: <input name="width" type="text" /> ft.<br>
        Radius: <input name="radius" type="text" /> ft.</br>
        <input type="submit" value="Calculate" name="calcarea_button">
    </form>
</div>

And $area is returned by this code elsewhere on the page:

<?php echo $area; ?> sq. ft.<br>

The bit where $area is filled by calcsquare works exactly how I intended, but no matter what I do, the else case returns 0. (I've set it to 50 initially as a troubleshooting measure, so I know it's being changed to 0 rather than not being changed at all.) The function calccirc is what I'm actually trying to accomplish, but you can see all the commented-out stuff I was using to try to make $area say something other than 0. I have no idea where that's even coming from. Again, calcsquare works fine every time I try it, so I know it ought to work!

Again, very new to PHP, please be gentle. Explain it like you would to your mom.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • 4
    `$_POST`, not `$POST`. – Jonnix Jan 15 '19 at 23:18
  • Is that `$POST` a typo or it's your actual code? If so, that falls under 2 votes to close. 1) As a typographical error. 2) [Undefined index/variable](https://stackoverflow.com/q/4261133/). – Funk Forty Niner Jan 15 '19 at 23:23
  • FYI on the duplicate. I chose to hammer this in order to avoid possible answers while the question was still open. – Funk Forty Niner Jan 15 '19 at 23:26
  • Welcome to SO! As your first question, aside from the typo and some unneeded sentences, it was a well written question. Best of luck learning PHP. – Will B. Jan 15 '19 at 23:34
  • So, why the typo would cause `$area` to be 0: `$POST["radius"]` is undefined. PHP treats undefined variables as having a value of `null`. When comparing with `==` rather than `===`, `null` is considered to be equal to `""`. So it's only ever going to go into the if part, so it will always do `calcsquare`, even when you've only posted a radius. (I assume if you post length and width as well as radius, you will no longer get zero.) What's for dinner? – Don't Panic Jan 15 '19 at 23:38
  • OH SON OF A - ....okay, that's definitely my error and thank you for spotting it, but how in the hell does that work? The bit with $POST["radius"] actually works! It's the other bit that doesn't! I have to run to work so I can't test it right now, I'll report back in later. – tiltingatwindmills Jan 16 '19 at 09:18
  • okay, yeah, that worked. Thanks and sorry. – tiltingatwindmills Jan 16 '19 at 18:24

0 Answers0