Wednesday 12 February 2014

PHP Calculator

PHP calculator v 0.1

under your www/ create a folder call 'calculator' next create a php file call index.php and paste this in and save it.

<?php
// Tek Komunity https://www.facebook.com/groups/tekkomunity/
 require("classes/Calculator.php"); ?> //we are pulling out all our operator this php class
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>T.K Calculator Ver 0.1</title>
</head>
<body>
<?php
$number1 = 784;  //this is our fisrt number
$number2 = 345; //secound number to round with
$operator = "/"; //variable for operator and it "/"
$calculator = new Calculator(); //we get a new operation
$calculator->setNumbers($number1,$number2); // we set our operator by defining tow number to rund wit
$calculator->setOperator($operator); //opertaor is set
$calculator->calculate(); //set to Calculator function to round these tow number
echo $number1." ".$operator." ".$number2." = ".$calculator->getOutput(); //We print out our first number and second number and our operator and answer which in our calculate function
?>
</body>
</html>

Inside our calculator folder create a new folder call classes and create a PHP file cal Calculator.php and copy this php code and paste.



<?php

class Calculator // this class we call it Calculator
{
        private $number1, $number2, $operator, $output; //we set our variable for storing our datas

        public function setNumbers($number1,$number2) //public function to get our number from index.php
        {
                $this->number1 = $number1;
                $this->number2 = $number2;
        }

        public function setOperator($operator) //function to set our operator
        {
                $this->operator = $operator;
        }

        public function calculate() //function to set our calculate
        {
                if($this->operator == "+") 
                        $this->output = $this->number1 + $this->number2;
                elseif($this->operator == "-")
                        $this->output = $this->number1 - $this->number2;
                elseif($this->operator == "*")
                        $this->output = $this->number1 * $this->number2;
                elseif($this->operator == "/")
                        $this->output = $this->number1 / $this->number2;
                else
                        $this->output = "An Error Has Occurred! Please get help from Tek Komunity"; // Else if any //thing goes wrong
        }

        public function getOutput() //finally output where we call in index.php
        {
                return $this->output; 
        }
}

?>


You can put this into a form and buttons like calculator and this is will be Version 2



No comments:

Post a Comment