newbe123 Posted November 18, 2010 Share Posted November 18, 2010 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"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/219085-how-to-use-function/ Share on other sites More sharing options...
Chris92 Posted November 18, 2010 Share Posted November 18, 2010 PHP isn't Perl. function parimeter($length, $width) { return 2 * ($length + $width); } $parimeter = parimeter($length, $width); echo "parimeter: . $parimeter"; Quote Link to comment https://forums.phpfreaks.com/topic/219085-how-to-use-function/#findComment-1136100 Share on other sites More sharing options...
newbe123 Posted November 18, 2010 Author Share Posted November 18, 2010 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"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/219085-how-to-use-function/#findComment-1136127 Share on other sites More sharing options...
litebearer Posted November 18, 2010 Share Posted November 18, 2010 you haven't asssigned any values to the length and with variables Quote Link to comment https://forums.phpfreaks.com/topic/219085-how-to-use-function/#findComment-1136128 Share on other sites More sharing options...
newbe123 Posted November 18, 2010 Author Share Posted November 18, 2010 thank you. I am aware of that but the thing is that this should work like a calculator and I should not have to specify values such as parimeters (4.2) I hope I have explained well enough Quote Link to comment https://forums.phpfreaks.com/topic/219085-how-to-use-function/#findComment-1136177 Share on other sites More sharing options...
litebearer Posted November 18, 2010 Share Posted November 18, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/219085-how-to-use-function/#findComment-1136185 Share on other sites More sharing options...
newbe123 Posted November 18, 2010 Author Share Posted November 18, 2010 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"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/219085-how-to-use-function/#findComment-1136192 Share on other sites More sharing options...
litebearer Posted November 18, 2010 Share Posted November 18, 2010 <?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; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/219085-how-to-use-function/#findComment-1136199 Share on other sites More sharing options...
newbe123 Posted November 18, 2010 Author Share Posted November 18, 2010 OMG, THANK YOU SOOOOOO MUCH. IT WORKS THANK YOU Quote Link to comment https://forums.phpfreaks.com/topic/219085-how-to-use-function/#findComment-1136205 Share on other sites More sharing options...
litebearer Posted November 18, 2010 Share Posted November 18, 2010 You are welcome Quote Link to comment https://forums.phpfreaks.com/topic/219085-how-to-use-function/#findComment-1136208 Share on other sites More sharing options...
newbe123 Posted November 23, 2010 Author Share Posted November 23, 2010 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/219085-how-to-use-function/#findComment-1138411 Share on other sites More sharing options...
newbe123 Posted November 23, 2010 Author Share Posted November 23, 2010 <?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 ()"; ? Quote Link to comment https://forums.phpfreaks.com/topic/219085-how-to-use-function/#findComment-1138414 Share on other sites More sharing options...
litebearer Posted November 23, 2010 Share Posted November 23, 2010 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 ; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/219085-how-to-use-function/#findComment-1138416 Share on other sites More sharing options...
newbe123 Posted November 23, 2010 Author Share Posted November 23, 2010 So I was almost right but thank you thank you and thank you Quote Link to comment https://forums.phpfreaks.com/topic/219085-how-to-use-function/#findComment-1138421 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.