This tutorial will add up two values that have been entered within the web page, I am going to use the web page functional aspects rather than the command line interface because php is mainly used within websites.
The web page itself is using a form tag, this form tag allows for the web page to post data back to the server, where the php will use the posts ($_POST) array ( [
The source code is
<?php $value1 = $_POST['value1']; $value2 = $_POST['value2']; ?> <html> <title>PHP - Add two numbers</title> <body> <form action="addtwonumbers.php" method="post"> <input type="text" name="value1" value="0" /> <input type="text" name="value2" value="0" /> <input type="submit" value="Calculate values"/> </form> Answer : <?php echo ($value1 + $value2); ?> </body> </html> |
if you save that as addtwonumbers.php within the php website configured directory and then open up that page via a web browser (e.g. http://localhost/addtwonumbers.php, localhost means the local pc, just like 127.0.0.1 is the local ring e.g the pc itself to talk to itself via an IP).
Just for completeness, this is the command line interface code.
<?php echo "Please enter value 1 : "; fscanf(STDIN, "%d\n", $value1); // reads number from STDIN standard input echo "Please enter value 2 : "; fscanf(STDIN, "%d\n", $value2); echo "Answer : " .($value1 + $value2) . "\n"; ?> |
Thanks for giving this. Even this simple code will be helpful for me because i am a fresher in programming field..
thank you for giving the example
Thanks for this… But, how about if the input is coming from an html textbox? Do i have to use fscanf function?
fscanf is more for files than HTML code.
Notice: Undefined index: value1 in C:\wamp\www\demo\add.php on line 2
Notice: Undefined index: value2 in C:\wamp\www\demo\add.php on line 3
have not tried out wamp before, but I am guessing this should work from the webpage with wamp
should work fine.
hii, i am new in php…i want to know if i can add two numbers from two different forms….as i have retrieved the value from db … in form 1, i have value 2 and in 2nd form…i have value 6, now i want to add this two value…how can i do it??
Hi Sidd
Something like
Genux
nice post for php beginners also check out my blog http://www.coderetina.com/addition-in-php/
showing like this:-in my program:-
Undefined index: value1 and value2 in line number like that……….
Hi, I am getting the result but initially it was showing undefined variables value 1 and value 2
hii, i am new in php. i want array form with continuesly 2 numbers add in the program to implement by a moodle tool. so i dnt know how?
Hi, I am sorry but not really sure what you are wanting.
If you are wanting a moodle tool (plugin / module update) etc then yes this can be done.
but not sure what you are meaning by an array form with “continuously 2 numbers add ?”
thanku for the codes
How to execute php script from command line interface?
if you have php installed and within the “PATH” of the command line interface, then just type
php
hello i want to add two values from 1 diff input.. and the total will show in total input… heres my code..
Load:
Dutty Allow:
Total:
sorry but there is no code there.
This tutorial will add up two values that have been entered within the web page, I am going to use the web page functional aspects rather than the command line interface because php is mainly used within websites.