Jump to content

Calculate area of a rectangle?


newbe123

Recommended Posts

the idea is that the function to calculate the area to be used inside the function perimeter, so that the perimeter function handles all printing.

 

This is what I've done but it's not the same as what I have mentioned above.

What Should I do?

 

 

This is what I want to do

 

Create a method called "Area ()" that calculates the area of a rectangle.

Call the method "Area ()" method from within "perimeter ()", submit

length and width as an argument to the method "Area ()" and use these to

calculate the area. Modify Approach "perimeter () "so that it prints the perimeter

and area when the user presses the button "CALCULATE". No printing will be done in

Approach "Area () "

 

 

 

<body>

	<h1>PHP-sida 4</h1>

	<form action="php4.php" method="post">

		<p>Längd: <input type="text" name="length" size="20" /></p>

		<p>Bredd: <input type="text" name="width" size="20" /></p>

		<p><input type="submit" value="CALCULATE" name="calc" /></p>

	</form>




<?php



if (isset($_POST['calc']))
{
$length= $_POST['length'];
$width= $_POST['width'];
echo "length:" . $length. "<br><br>";
echo "width:" . $width. "<br><br>";


function perimeter($length, $width)
{

return  2* ($length+ $width);

}
$perimeter= perimeter($length, $width);

echo "perimeter:" . $perimeter. "<br>", "<br>";

function area($length, $width)
{
return $length* $width;
}
$area = area($length, $width);
echo "area:" . $area ;

}

?>



</body>

Link to comment
Share on other sites

do you mean...

$length=20;
$width=10;
function area($a,$b) {
  return $a*$b;
}
function perimeter($a,$b) {
  echo "Length: " . $a . "<br>":
  echo "Width  : " . $b . "<br>";
  echo "Perimeter: " .  (2 * ($a + $b)) . "<br>";
  echo "Area : " . area($a,$b);
  return;
}

  
}

Link to comment
Share on other sites

Not sure what you mean by

I am  not allowed to give the length and the width.

How are the functions supposed to work if they are not supplied the necessary information?

 

BTW - I had a small typo above - here is corrected and TESTED version (it DOES work)

<?PHP

function area($a,$b) {
  return $a*$b;
}

function perimeter($a,$b) {
  echo "Length: " . $a . "<br>";
  echo "Width  : " . $b . "<br>";
  echo "Perimeter: " .  (2 * ($a + $b)) . "<br>";
  echo "Area : " . area($a,$b);
  return;
}
$length = 10;
$width = 5;
echo perimeter($length,$width);
?>

Link to comment
Share on other sites

Create a method called "perimeter ()" that calculates the perimeter of a rectangle. The function to receive the two parameters "long" and "width" of a form. perimeter shall then be printed on the page when the user presses a button titled "CALCULATE".

 

Create a method called "Area ()" that calculates the area of a rectangle.

Call the method "Area ()" method from within "perimeter ()", submit

length and width as an argument to the method "Area ()" and use these to

calculate the area. Modify Approach "perimeter () "so that it prints the perimeter

and area when the user presses the button "CALCULATE". No printing will be done in

Approach "Area () "

 

 

It's like this that the person that visits this page is going to input any number and that presses the button calculate and than the output

is like:

 

length: the number given by the person

width: the number given by the person

perimeter: answer

area: answer

Link to comment
Share on other sites

this is what I mean

 

<?php



if (isset($_POST['calc']))
{



$length= $_POST['length'];



$width= $_POST['width'];



echo "length:" . $length. "<br><br>";



echo "width:" . $width. "<br><br>";

 

 

 

 

I am not allowed to give the

$length = 10;

$width = 5;

 

 

It's like this anyone that going to see this page can give what ever length and what ever width that they want in the text-box and than enters the calculate button and gets the length, the width that he/she gave and the area and the perimeter that was calculated.

Link to comment
Share on other sites

You need to merge your code with what has been provided...

<?php
// FUNCTIONS //

function area($a,$b) {
  return $a*$b;
}

function perimeter($a,$b) {
  echo "Length: " . $a . "<br/>";
  echo "Width  : " . $b . "<br/>";
  echo "Perimeter: " .  (2 * ($a + $b)) . "<br/>";
  echo "Area : " . area($a,$b);
  return;
}

?>
<html>
<body>
	<h1>PHP-sida 4</h1>
	<form action="php4.php" method="post">
		<p>Längd: <input type="text" name="length" size="20" /></p>
		<p>Bredd: <input type="text" name="width" size="20" /></p>
		<p><input type="submit" value="CALCULATE" name="calc" /></p>
	</form>
	<?php

	if (isset($_POST['calc']) && !empty($_POST['length']) && !empty($_POST['width'])) {
		echo 'Processing...<br/>';
		echo perimeter($_POST['length'],$_POST['width']);
	} else {
		echo 'You need to specify a length and a width.';
	}

	?>
</body>
</html>

Link to comment
Share on other sites

  • 4 years later...

excuse me..

how to do this one?

Write the rectangle area function that accepts user input. Present a form to the user with the message "Please enter the values of the length and width of your rectangle." Below this, supply two text boxes, one for length and one for width. Using your function to process the user supplied values, return the result statement from the previous exercise to the user. Reminder: the statement was "A rectangle of length $l and width $w has an area of $area.", where $l and $w are the arguments and $area is the result.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.