djl1990 Posted October 13, 2011 Share Posted October 13, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/249076-im-a-beginner-basic-php-calculator/ Share on other sites More sharing options...
jcbones Posted October 13, 2011 Share Posted October 13, 2011 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'. Quote Link to comment https://forums.phpfreaks.com/topic/249076-im-a-beginner-basic-php-calculator/#findComment-1279167 Share on other sites More sharing options...
djl1990 Posted October 13, 2011 Author Share Posted October 13, 2011 whats the htdoc folder mate? Quote Link to comment https://forums.phpfreaks.com/topic/249076-im-a-beginner-basic-php-calculator/#findComment-1279168 Share on other sites More sharing options...
djl1990 Posted October 13, 2011 Author Share Posted October 13, 2011 also i created the file in notepad is this okay? should my file extension be .html or .php ? Quote Link to comment https://forums.phpfreaks.com/topic/249076-im-a-beginner-basic-php-calculator/#findComment-1279170 Share on other sites More sharing options...
jcbones Posted October 13, 2011 Share Posted October 13, 2011 extension should be .php Xamp Start-up list <-follow this page from start to bottom, if you still have questions, come back I'll be glad to help you. Quote Link to comment https://forums.phpfreaks.com/topic/249076-im-a-beginner-basic-php-calculator/#findComment-1279173 Share on other sites More sharing options...
djl1990 Posted October 13, 2011 Author Share Posted October 13, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/249076-im-a-beginner-basic-php-calculator/#findComment-1279179 Share on other sites More sharing options...
djl1990 Posted October 13, 2011 Author Share Posted October 13, 2011 I just went to run the following code through Wamp Server and im getting the following line: Parse error: syntax error, unexpected T_STRING in C:\wamp\www\Web Development\calculator.php on line 44 not a good start Quote Link to comment https://forums.phpfreaks.com/topic/249076-im-a-beginner-basic-php-calculator/#findComment-1279182 Share on other sites More sharing options...
jcbones Posted October 14, 2011 Share Posted October 14, 2011 Post your code here, please put them inside of the ... brackets Quote Link to comment https://forums.phpfreaks.com/topic/249076-im-a-beginner-basic-php-calculator/#findComment-1279183 Share on other sites More sharing options...
djl1990 Posted October 14, 2011 Author Share Posted October 14, 2011 /* ====================================================== 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/249076-im-a-beginner-basic-php-calculator/#findComment-1279305 Share on other sites More sharing options...
djl1990 Posted October 14, 2011 Author Share Posted October 14, 2011 Did anyone manage to spot the problem? Quote Link to comment https://forums.phpfreaks.com/topic/249076-im-a-beginner-basic-php-calculator/#findComment-1279384 Share on other sites More sharing options...
TOA Posted October 14, 2011 Share Posted October 14, 2011 I noticed you don't have closing php tags here x = <input type="text" name="x" size="5" value="<?php print $x; "/> y = <input type="text" name="y" size="5" value="<?php print $y; "/> Quote Link to comment https://forums.phpfreaks.com/topic/249076-im-a-beginner-basic-php-calculator/#findComment-1279386 Share on other sites More sharing options...
djl1990 Posted October 14, 2011 Author Share Posted October 14, 2011 I dont understand because ive copied and pasted the code from my tutors which works fine. Quote Link to comment https://forums.phpfreaks.com/topic/249076-im-a-beginner-basic-php-calculator/#findComment-1279410 Share on other sites More sharing options...
djl1990 Posted October 14, 2011 Author Share Posted October 14, 2011 Heres the actual Error Message: Parse error: syntax error, unexpected T_STRING in C:\wamp\www\Web Development\calculator.php on line 44 What is T_STRING? Quote Link to comment https://forums.phpfreaks.com/topic/249076-im-a-beginner-basic-php-calculator/#findComment-1279412 Share on other sites More sharing options...
djl1990 Posted October 14, 2011 Author Share Posted October 14, 2011 DevilsAdvocate do you know where the closing tags should be? regards. Dan Quote Link to comment https://forums.phpfreaks.com/topic/249076-im-a-beginner-basic-php-calculator/#findComment-1279413 Share on other sites More sharing options...
jcbones Posted October 14, 2011 Share Posted October 14, 2011 x = <input type="text" name="x" size="5" value="<?php print $x; ?>"/> y = <input type="text" name="y" size="5" value="<?php print $y; ?>"/> Quote Link to comment https://forums.phpfreaks.com/topic/249076-im-a-beginner-basic-php-calculator/#findComment-1279424 Share on other sites More sharing options...
djl1990 Posted October 14, 2011 Author Share Posted October 14, 2011 Thanks, multiplication is working now. Could anyone tell me what the 'isset' i n the if statement is? Quote Link to comment https://forums.phpfreaks.com/topic/249076-im-a-beginner-basic-php-calculator/#findComment-1279470 Share on other sites More sharing options...
TOA Posted October 14, 2011 Share Posted October 14, 2011 The isset checks to see if the $calc variable is set, which it won't be until the form is submitted, so in this context, it's being used to control when the code runs to process the form submission. Quote Link to comment https://forums.phpfreaks.com/topic/249076-im-a-beginner-basic-php-calculator/#findComment-1279480 Share on other sites More sharing options...
KevinM1 Posted October 14, 2011 Share Posted October 14, 2011 @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. Quote Link to comment https://forums.phpfreaks.com/topic/249076-im-a-beginner-basic-php-calculator/#findComment-1279482 Share on other sites More sharing options...
djl1990 Posted October 14, 2011 Author Share Posted October 14, 2011 This will take ages to learn off by heart, can someone help on adding the other 3 operations. Also what do you think the requirement is? A drop down box for each operator or something different? Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/249076-im-a-beginner-basic-php-calculator/#findComment-1279505 Share on other sites More sharing options...
Pikachu2000 Posted October 14, 2011 Share Posted October 14, 2011 We aren't here to do your homework for you. Quote Link to comment https://forums.phpfreaks.com/topic/249076-im-a-beginner-basic-php-calculator/#findComment-1279517 Share on other sites More sharing options...
djl1990 Posted October 14, 2011 Author Share Posted October 14, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/249076-im-a-beginner-basic-php-calculator/#findComment-1279518 Share on other sites More sharing options...
KevinM1 Posted October 14, 2011 Share Posted October 14, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/249076-im-a-beginner-basic-php-calculator/#findComment-1279525 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.