Show DevBest [PHP] PHP calculator

Status
Not open for further replies.

habbz

Custom Title bitchssssssss
Nov 12, 2010
227
1
Well I've been learning PHP for about 4 months now and I made this PHP calculator:
PHP:
   <?php

$page = $_GET['page'];


class calc {
     var $number1;
     var $number2;

          function add($number1,$number2)
          {
                   $result =$number1 + $number2;
                    echo("The sum of $number1 and $number2 is $result<br><br>");
                    echo("$number1 + $number2 = $result");
                    exit;
           }

          function subtract($number1,$number2)
          {
                   $result =$number1 - $number2;
                    echo("The difference of $number1 and $number2 is $result<br><br>");
                    echo("$number1 &#045 $number2 = $result");
                    exit;
           }

          function divide($number1,$number2)
          {
                   $result =$number1 / $number2;
                    echo("$number1 divided by $number2 is $result<br><br>");
                    echo("$number1 ÷ $number2 = $result");
                    exit;
           }

          function multiply($number1,$number2)
          {
                   $result =$number1 * $number2;
                    echo("The product of $number1 and $number2 is $result<br><br>");
                    echo("$number1 x $number2 = $result");
                    exit;
           }
}
$calc = new calc();
?>
<TITLE>PHP Calculator v1</TITLE>
<form name="calc" action="?page=calc" method="POST">
Number 1: <input type=text name=value1><br>
Number 2: <input type=text name=value2><br>
Operation: <input type=radio name=oper value="add">Addition <input type=radio name=oper value="subtract">Subtraction <input type=radio name=oper value="divide">Division <input type=radio name=oper value="multiply">Multiplication</input><br>
<input type=submit value="Calculate">
</form>
<?php
if($page == "calc"){
$number1 = $_POST['value1'];
$number2 = $_POST['value2'];
$oper = $_POST['oper'];
     if(!$number1){
          echo("You must enter number 1!");
          exit;
     }
     if(!$number2){
          echo("You must enter number 2!");
          exit;
     }
     if(!$oper){
          echo("You must select an operation to do with the numbers!");
          exit;
     }
     if(!eregi("[0-9]", $number1)){
          echo("Number 1 MUST be numbers!");
          exit;
     }
     if(!eregi("[0-9]", $number2)){
          echo("Number 2 MUST be numbers!");
          exit;
     }
     if($oper == "add"){
          $calc->add($number1,$number2);
     }
     if($oper == "subtract"){
          $calc->subtract($number1,$number2);
     }
     if($oper == "divide"){
          $calc->divide($number1,$number2);
     }
     if($oper == "multiply"){
          $calc->multiply($number1,$number2);
     }
}
?>
Demo:
Any feed back is appreciated

Thanks :)
 

Sledmore

Chaturbate Livestreamer
Staff member
FindRetros Moderator
Jul 24, 2010
5,199
3,934
You sure you made this?

I only found it on about 10 websites, example;



- But nice, share.
 

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,638
2,393
I'm not sure if you wrote this and released it on that website too, but if you never wrote this script, at least credit the original author.

Also, you could improve this by using 'is_numeric' around the values to check if they actually are numbers and don't contain letters and/or symbols.
 
Status
Not open for further replies.

Users who are viewing this thread

Top