No php output


#1

Hi,
I am trying to do this simple program that should calculate the circumference and the area of a rectangle. It should be two different functions and but the output should come from only 1 function. Input comes from a form. What I am having trouble with atm is: The echo does not show if I have it inside the first function and does not print any value if I place it outside the function.

$lenght = $_POST[“lenght”];
$width = $_POST[“width”];

function calculateCircumference($lenght, $width){

$arean=calculateArea($lenght, $width);
$omkrets = $lenght * 2 + $width * 2;

echo "Arean av rektangeln är12313: $arean <br> ";
echo "omkretsen av rektangeln är: $omkrets";

}

function calculateArea($lenght, $width) {

$arean = $lenght * $width;
return $arean;

}

----- My form class below --------

Skriv in längd och bredd för att beräkna omkretsen av rektangel!


längd
bredd

Thanks a lot

//Struggler


#2

The main points of functions are they allow code to be reused and to simplify the calling program logic.

Functions consist of two parts, the function definition and the function call. Function definitions should be placed near the start of your file, or even better in a separate file that gets required whenever the function definition(s) are needed.

Functions should return their result to the calling code, The function call should replace the original in-line code that was moved into the function definition. Each function should only accomplish one goal. Functions should not echo output (it’s the responsibility of the calling code to know what to do with the result from the function.) You must call a function in order to use it (your current code isn’t calling the calculateCircumference() function at all.)

Your main code should first detect that the form was submitted (you should actually be using a get method form if you are causing a web page to be displayed), validate all the input data (what should your code do if an empty or non-numeric value was submitted), if there are no validation errors use the submitted data, then finally echo the result. At the point that you need to calculate the Circumference or the Area, you would call the appropriate function.