Jump to content

Recommended Posts

First of all hi everyone, this is my first post as you can see. And i have my first task in PHP, now i warn you i have no experience or know-how whatsover in PHP so please bear with me.

 

My first task is to extend some PHP code to add some functions. the code weve been given already is capable of mutiplication of two variables x and y.

 

Here it is anyway.

 

1. View the code and copy/paste it into an editor and save it to your own workspace.

 

2. Extend the script to perform the four basic arithmetic functions : +, -, *, / and return a result.

 

3. Extend the script again to move the calculations into a function and make use of this function.

 

 

<?php

/* ======================================================
   PHP Calculator example using "sticky" form (Version 1)
   ======================================================

   Author : P Chatterjee (adopted from an original example written by C J Wallace)

   Purpose : To multiply 2 numbers passed from a HTML form and display the result.

   input:
      x, y : numbers
      calc : Calculate button pressed


   Date: 15 Oct 2007

*/

// grab the form values from $_HTTP_POST_VARS hash
extract($_GET);

// first compute the output, but only if data has been input
   if(isset($calc)) { // $calc exists as a variable
      $prod = $x * $y;
   }
   else { // set defaults
      $x=0;
      $y=0;
      $prod=0;
   }
?>

<html>
   <head>
      <title>PHP Calculator Example</title>
   </head>

   <body>

      <h3>PHP Calculator (Version 1)</h3>
      <p>Multiply two numbers and output the result</p>

      <form method="get" action="<?php print $_SERVER['PHP_SELF']; ?>">

         x = <input type="text" name="x" size="5" value="<?php print $x; ?>"/>
         y =  <input type="text" name="y" size="5" value="<?php  print $y; ?>"/>


         <input type="submit" name="calc" value="Calculate"/>
         <input type="reset" name="clear" value="Clear"/>
      </form>

      <!-- print the result -->
      <?php if(isset($calc)) {

         print "<p>x * y = $prod</p>";

      } ?>

   </body>
</html> 

 

 

 

Firstly how do i run this code, i have Xamp installed if thats any good?

Secondly, do i need two seperate files. i.e. calculator.php for the php code and calculator.html for the form?

Thirdly i have no idea how to implement the divide, add and subtract functions. should i do a drop down menu where the user should select what operator they want? or is it more simple than that.

 

Any help greatly appreciated.

 

 

Please let me know if this post is okay. 

Link to comment
https://forums.phpfreaks.com/topic/249076-im-a-beginner-basic-php-calculator/
Share on other sites

Arithmetic Operators for PHP

 

You can keep it all in one file.  PHP can mix with HTML.  Xamp or WAMPServer, or anything that will parse PHP code is all that is needed.  Put it in your htdoc folder, then open up your browser and type 'localhost/file.php'.

Yes it seems im okay on opening up the pages by running the Wamp server.

 

I have a couple of questions on the code.

 

I assume the variable they've already created: $prod is short for product and stores the value of $x * $y , anyway ive changed it to $product and will use proper names to make it easier to understand.

 

I have created three more variables: $difference, $sum and $quotient for storing the results of subtract, add and divide respectively.

 

I noticed there is an if statement. im stroggling to see what 'isset' represents ?

Also is else if and else statements required for the other 3?

/* ======================================================
   PHP Calculator example using "sticky" form (Version 1)
   ======================================================

   Author : P Chatterjee (adopted from an original example written by C J Wallace)

   Purpose : To multiply 2 numbers passed from a HTML form and display the result.

   input:
      x, y : numbers
      calc : Calculate button pressed


*/


// grab the form valus from $_HTTP_POST_VARS hash
extract ($_GET);

// first compute the output, but only if data has been input
if(isset($calc)) { // $calc exists as a variable
	$prod = $x * $y;
}
else { // set of defaults
	$x=0;
	$y=0;
	$prod=0;
}
?>

<html>
<head>
	<title> PHP Calculator Example</title>
</head>

<body>

<h3>PHP Calculator (Version 1)</h3>
<p>Multiply 2 numbers and output the result</p>

<form method="get" action = "<?php print $_SERVER['PHP_SELF']; ?>">

	x = <input type="text" name="x" size="5" value="<?php print $x; "/>
	y = <input type="text" name="y" size="5" value="<?php print $y; "/>

	<input type="submit" name="calc" value="Calculate"/>
	<inpt type="reset" name="clear" value="Clear"/>

</form>

<!-- print the results -->
<?php if(isset($calc)) {
	print "<p>x * y = $prod</p>";
} ?>

@djl1990 Whenever you're curious about a built-in PHP function, you should take a look at it in the manual: http://www.php.net/quickref.php  Chances are, you'll see a use case example in the documentation that will help your overall picture come into focus.

And im not expecting that at all. But my main question is how do i implement the 3 other operations, what kind of loops should i use? and how should the user be able to select what operation they want to carry out? i.e. 4 buttons with the values: (-, +, *, / ) or a drop down menu with the 4 operator symbols?

 

thanks.

You could do either one.  Hell, you could simply have a textbox for users to write an entire infix expression and parse it with PHP on the back end.  It all depends on what you want and what you think is the best design.

 

Like Pikachu said, we're not in the business of doing homework for students.  That includes giving the kind of advice which would minimize what individual assignments are trying to teach.

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.