Jump to content

how to use function!????


newbe123

Recommended Posts

HI

 

I'm having problem with my function.

 

This is what I want to do:

 

 

Create a method that calculates the parimeter of a

rectangle.

create a method that calculates the area of the rectangle and

call the method "area()" from the method "perimeter()", send

length and width as an argument to the method area and use these to

calculate the area. Modify the methods so that it prints the parimeter and area.

 

And this is my code, what have I done wrong and what should I do? please help.

 

 

<body>

 

<h1>PHP-sida 4</h1>

 

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

 

<p>Length: <input type="text" name="length" size="20" /></p>

 

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

 

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

 

</form>

 

 

 

 

<?php

 

 

 

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

{

 

echo "Length:";

echo $_POST['length'], "<br>", "<br>";

 

echo "width:";

echo $_POST['width'], "<br>", "<br>";

 

 

function parimeter()

{

parimeter($length, width);

{

$parimeter = 2* ($length+ $width);

 

 

{

return $parimeter;

}

$parimeter = parimeter($length, $width);

echo "parimeter: . $parimeter";

}

 

}

 

?>

 

Link to comment
Share on other sites

thank you but the output of the parimeter will be 0! why? I want it to count the given length and width = parimeter.

 

 

 

<?php

 

 

 

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

{

 

echo "Length:";

echo $_POST['length'], "<br>", "<br>";

 

echo "width:";

echo $_POST['width'], "<br>", "<br>";

 

 

function parimeter($length, $width)

{

    return 2 * ($length + $width);

}

 

$parimeter = parimeter($length, $width);

echo "parimeter: . $parimeter";

?>

 

 

 

Link to comment
Share on other sites

a calculator needs to have buttons pushed. if you assign no value to a variable it essentially contains ZERO; therefore the result of calculating with either return 0 or a division error or some possible unknown un-reliable result

 

For your function to do its thing you MUST have values in the variables

Link to comment
Share on other sites

if you look at my <form> you see the "user" gives the length  let say 22 and the width 23 and than pushes the button to get the length for 22 and width 23 and than with use of function parimeter() I want it to calculate the parimeter of this rectangule with given length and width.

 

 

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

 

        <p>Length: <input type="text" name="length" size="20" /></p>

 

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

 

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

 

      </form>

 

<?php

 

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

{

echo "Length:";

echo $_POST['length'], "<br>", "<br>";

 

echo "width:";

echo $_POST['width'], "<br>", "<br>";

 

function parimeter($length, $width)

{

    return 2 * ($length + $width);

}

 

$parimeter = parimeter($length, $width);

echo "parimeter: . $parimeter";

?>

Link to comment
Share on other sites

<?php
if (isset($_POST['calc'])){
  /* MUST DO THIS TO MAKE IT WORK */
  $length = $_POST['length'];
  $width = $_POST['width'];

  echo "Length: " . $length . "<br><br":
  echo "Width: " . $width . "<br><br>";
  function parimeter($length, $width){
    return 2 * ($length + $width);
}
$parimeter = parimeter($length, $width);
echo "parimeter: " . $parimeter;
}
?>

Link to comment
Share on other sites

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

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

length and width as an argument to the method "Area () "and use these to calculate the Area. Modify the method "parimeter ()" so that it prints perimeter and area when the user presses the button "CALCULATE".

 

 

The Area must be calculated with the help of a function, this must be used inside the function of parimeter.

 

I've done it this way but it should not be so. It works and you get the area but it should be calculated like the way that i've mentioned above.

 

 

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

 

<p>Length: <input type="text" name="length" size="20" /></p>

 

<p>Width: <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 parimeter($length, $width)

{

 

return  2* ($length + $width);

 

}

$parimeter = parimeter($length, $width);

 

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

 

}

if ($_POST['calc'] == "calculate")

{

 

$width = $_POST['width'];

 

$length = $_POST['length'];

 

$area = $width*$length;

 

print "area: $area <b>";

}

 

?>

 

 

 

 

Link to comment
Share on other sites

<?php

 

 

 

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

{

$langd = $_POST['langd'];

$bredd = $_POST['bredd'];

echo "Langd:" . $langd . "<br><br>";

echo "Bredd:" . $bredd . "<br><br>";

 

 

function omkrets($langd, $bredd)

{

 

return  2* ($langd + $bredd);

 

}

$omkrets = omkrets($langd, $bredd);

 

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

 

}

 

function area($langd, $bredd)

{

return $langd * $bredd;

}

$area = area($langd, $bredd);

echo "area:" . $area ;

 

?>

 

I know this is not right either, but all is well no errors?

How can I call the method "Area ()" From Within the method "perimeter ()"; ?

Link to comment
Share on other sites

Perhaps...

<?php
if (isset($_POST['calc'])){
$langd = $_POST['langd'];
$bredd = $_POST['bredd'];
echo "Langd:" . $langd . "<br><br>";
echo "Bredd:" . $bredd . "<br><br>";

function omkrets($langd, $bredd){
	return 2* ($langd + $bredd);
}
$omkrets = omkrets($langd, $bredd);
echo "omkrets:" . $omkrets . "<br>", "<br>";
function area($langd, $bredd){
	return $langd * $bredd;
}
$area = area($langd, $bredd);
echo "area:" . $area ;
}


?>

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.