Calculating Area with PHP

PHP inlog code
Zmeel Photography / Getty Images
01
of 03

Collecting User Information

 

Calculate Area

Calculate Area

Width:
Length:

Write the HTML to collect the length and width of a rectangle from the user. This document uses PHP_SELF to send the information back to this page when the user submits his data. The script solicits three variables -- length, width, and calc. The calc variable is hidden and will be used in the next step of this process.

02
of 03

Doing the Math

 Results";
 print "The area of a rectangle $width x $length is $area 

"; } ?>

This PHP script inserts under the

tag, and above the first

tag. This is the code that completes the calculation. This code is only executed if the calc variable exists, so if the user has not submitted a form yet, this code is ignored.

 

The PHP collects the length and width variables and then multiplies them. It returns the answer to the user. The original form remains below so the user can complete another calculation if they need.

03
of 03

Full Code

The full script that integrates the PHP-based calculation with the HTML-based user-input wrapper appears below

 

Calculate Area

Results"; print "The area of a rectangle $width x $length is $area

"; } ?>

Calculate Area

Width:
Length:

Format
mla apa chicago
Your Citation
Bradley, Angela. "Calculating Area with PHP." ThoughtCo, Aug. 30, 2017, thoughtco.com/calculating-area-with-php-p2-2693841. Bradley, Angela. (2017, August 30). Calculating Area with PHP. Retrieved from https://www.thoughtco.com/calculating-area-with-php-p2-2693841 Bradley, Angela. "Calculating Area with PHP." ThoughtCo. https://www.thoughtco.com/calculating-area-with-php-p2-2693841 (accessed January 21, 2018).