zheka007 Posted May 1, 2009 Share Posted May 1, 2009 Hello. I am a beginner in PHP. Can you please assist me. I need to create a script to add two number together. The numbers should come from a web form (HTML) and be sent to the PHP script. The PHP script shoud then add the two numbers together and display the result. Thank you very much in advance!!! Quote Link to comment https://forums.phpfreaks.com/topic/156358-adding/ Share on other sites More sharing options...
Ken2k7 Posted May 1, 2009 Share Posted May 1, 2009 Are you asking someone to help you with this or make it for you? Quote Link to comment https://forums.phpfreaks.com/topic/156358-adding/#findComment-823227 Share on other sites More sharing options...
zheka007 Posted May 1, 2009 Author Share Posted May 1, 2009 if you could please, i have no clue how to do this. I need it for my website. Thank you very much, I really appreciate it Quote Link to comment https://forums.phpfreaks.com/topic/156358-adding/#findComment-823231 Share on other sites More sharing options...
corbin Posted May 1, 2009 Share Posted May 1, 2009 Sounds like homework to me. If it is, people on forums generally prefer for people to be upfront about something being homework. Anyway, the goal of this forum is to help people, not do things for people. (Well, the freelancing section is all about doing things for people, but chances are, you don't want to pay someone for something this simple.) You will want to read a tutorial on $_POST and basic PHP syntax. Here are a couple of links: http://www.tizag.com/phpT/ http://www.tizag.com/phpT/postget.php http://www.w3schools.com/php/php_post.asp Quote Link to comment https://forums.phpfreaks.com/topic/156358-adding/#findComment-823246 Share on other sites More sharing options...
Ken2k7 Posted May 1, 2009 Share Posted May 1, 2009 zheka007 - I hope you realize that this is for *HELP* not requests. Here's a basic outline (not all complete). something.html <!-- some HTML --> <form method="post" action="something.php"> Number 1: <input type="text" name="num1" /><br /> Number 2: <input type="text" name="num2" /> </form> <!-- some more HTML --> something.php <?php // check PHP version if it has filter_input() function. If not, use superglobals. $num1 = filter_input(INPUT_POST, 'num1', FILTER_VALIDATE_INT); $num2 = filter_input(INPUT_POST, 'num2', FILTER_VALIDATE_INT); $num1 = empty($num1)? filter_input(INPUT_POST, 'num1', FILTER_VALIDATE_FLOAT); $num2 = empty($num2)? filter_input(INPUT_POST, 'num2', FILTER_VALIDATE_FLOAT); if (empty($num1)) die("The first input value is not a number."); if (empty($num2)) die("The second input value is not a number."); echo $num1 + $num2; Again, finish the rest yourself. Quote Link to comment https://forums.phpfreaks.com/topic/156358-adding/#findComment-823248 Share on other sites More sharing options...
Daniel0 Posted May 1, 2009 Share Posted May 1, 2009 $left = $leftOrig = (int) $_POST['num1']; $right = $rightOrig = (int) $_POST['num2']; $c = $r = 0; for ($t = PHP_INT_MAX; (bool) $t; $t >>= 1) { $r <<= 1; $r |= ($left ^ $right ^ $c) & 1; $c = (($left | $right) & $c | $left & $right) & 1; $left >>= 1; $right >>= 1; } for ($t = PHP_INT_MAX, $c = ~$t; (bool) $t; $t >>= 1) { $c <<= 1; $c |= $r & 1; $r >>= 1; } echo "{$leftOrig} + {$rightOrig} = {$c}"; Quote Link to comment https://forums.phpfreaks.com/topic/156358-adding/#findComment-823385 Share on other sites More sharing options...
premiso Posted May 1, 2009 Share Posted May 1, 2009 Pay attention in class, read the chapter, and more importantly....TRY IT YOURSELF FIRST. PS: Daniel, that is the best bit of PHP Code I have ever seen written. That would definitely get me an A on this homework assignment if I turned it in! Quote Link to comment https://forums.phpfreaks.com/topic/156358-adding/#findComment-823467 Share on other sites More sharing options...
Ken2k7 Posted May 1, 2009 Share Posted May 1, 2009 Daniel0, that is awesome! zheka007 - If I were you now, I wouldn't pay attention to Daniel0's code. It'll just confuse the heck out of you. But if you want to read up on how his code works, learn about binary and bit shift. Quote Link to comment https://forums.phpfreaks.com/topic/156358-adding/#findComment-823484 Share on other sites More sharing options...
Mark Baker Posted May 1, 2009 Share Posted May 1, 2009 And just for luck, subtraction using Daniel's method: $left = $leftOrig = (int) $_POST['num1']; $right = $rightOrig = (int) $_POST['num2']; $right = PHP_INT_MAX ^ $right -1; $c = $r = 0; for ($t = PHP_INT_MAX; (bool) $t; $t >>= 1) { $r <<= 1; $r |= ($left ^ $right ^ $c) & 1; $c = (($left | $right) & $c | $left & $right) & 1; $left >>= 1; $right >>= 1; } for ($t = PHP_INT_MAX, $c = ~$t; (bool) $t; $t >>= 1) { $c <<= 1; $c |= $r & 1; $r >>= 1; } echo "{$leftOrig} - {$rightOrig} = {$c}"; Some of us remember using two's complement Quote Link to comment https://forums.phpfreaks.com/topic/156358-adding/#findComment-823601 Share on other sites More sharing options...
Mark Baker Posted May 1, 2009 Share Posted May 1, 2009 Could also use $right ^= PHP_INT_MAX; $right++; instead of $right = PHP_INT_MAX ^ $right -1; Quote Link to comment https://forums.phpfreaks.com/topic/156358-adding/#findComment-823610 Share on other sites More sharing options...
zheka007 Posted May 1, 2009 Author Share Posted May 1, 2009 Guys! Thank you very much. You are all awesome!!! Quote Link to comment https://forums.phpfreaks.com/topic/156358-adding/#findComment-823874 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.