[Release] [PHP] [OOP] Calculator

Results 1 to 8 of 8
  1. #1
    Developer iGalaxy is offline
    MemberRank
    Jul 2013 Join Date
    C:/xampp/htdocsLocation
    488Posts

    [Release] [PHP] [OOP] Calculator

    Hello everyone, I was practicing OOP PHP, and I ended up with an OOP PHP Calculator! So here is the source:

    Code:
    <?php
    class Calculater {
    	public function add($num1, $num2) {
    		$total = $num1 + $num2;
    		echo $total;
    	}
    	public function substract($num1, $num2) {
    		$total = $num1 - $num2;
    		echo $total;
    	}
    	public function multiply($num1, $num2) {
    		$total = $num1 * $num2;
    		echo $total;
    	}
    	public function divide($num1, $num2) {
    		$total = $num1 / $num2;
    		echo $total;
    	}
    }
    $Calculate = new Calculater;
    if(isset($_REQUEST['submit'])) {
    	$num1 = $_POST['num1'];
    	$num2 = $_POST['num2'];
    	if(!ctype_digit($num1)) {
    		echo "Error: Your inputs may only contain numbers";
    		exit();
    	}
    	if(!ctype_digit($num2)) {
    		echo "Error: Your inputs may only contain numbers";
    		exit();
    	}
    	$operation = $_POST['operation'];
    	if($operation == "Add") {
    		$Calculate->add($num1, $num2);
    	}
    	if($operation == "Substract") {
    		$Calculate->substract($num1, $num2);
    	}
    	if($operation == "Multiply") {
    		$Calculate->multiply($num1, $num2);
    	}
    	if($operation == "Divide") {
    		$Calculate->divide($num1, $num2);
    	}
    }
    ?>
    <form method="post" action="" name="submit">
    <input type="text" name="num1">
    <input type="text" name="num2">
    <select name="operation">
    	<option>Add</option>
    	<option>Substract</option>
    	<option>Multiply</option>
    	<option>Divide</option>
    </select>
    <input type="submit" name="submit" value="Submit">
    </form>
    Copy paste the above code in a *.php file and save it. In-order to run it you need an Apache server. Hope you like the release! Please give me ideas to release something the next time because I am always looking to code something and release it! Please give me feed back if you have ideas on improving my code!


  2. #2
    Pee Aitch Pee Dave is offline
    MemberRank
    Mar 2011 Join Date
    The NetherlandsLocation
    722Posts

    Re: [Release] [PHP] [OOP] Calculator

    - I don't really see how this is OOP, you only created a class with 4 functions.
    - You have to return in functions, not echo values.
    - $_REQUEST > $_POST.
    - Switch statement for the operation part may look neater.
    - Your <option>'s do not have any value bound to it. (<option value="add">).

    Make it so that I can do multiple calculations at once, instead of making it possible to add/subtract/multiply/divide 2 values only.

  3. #3
    Developer iGalaxy is offline
    MemberRank
    Jul 2013 Join Date
    C:/xampp/htdocsLocation
    488Posts

    Re: [Release] [PHP] [OOP] Calculator

    @Dave further explanation would be really good.

  4. #4
    Intelligent DoucheBag jur13n is offline
    MemberRank
    Jan 2008 Join Date
    Zwolle,Location
    1,946Posts

    Re: [Release] [PHP] [OOP] Calculator

    Quote Originally Posted by iGalaxy View Post
    @Dave further explanation would be really good.
    Further explanation:
    You're a fucking idiot if you can't understand what he said.
    And you claim to code OOP?

  5. #5
    Digital Horizon KristiansJ is offline
    MemberRank
    Jul 2012 Join Date
    203Posts

    Re: [Release] [PHP] [OOP] Calculator

    Quote Originally Posted by iGalaxy View Post
    Hello everyone, I was practicing OOP PHP, and I ended up with an OOP PHP Calculator!
    [/code]

    Copy paste the above code in a *.php file and save it. In-order to run it you need an Apache server. Hope you like the release! Please give me ideas to release something the next time because I am always looking to code something and release it! Please give me feed back if you have ideas on improving my code!

  6. #6
    Developer iGalaxy is offline
    MemberRank
    Jul 2013 Join Date
    C:/xampp/htdocsLocation
    488Posts

    Re: [Release] [PHP] [OOP] Calculator

    @jur13n I wanted to know about multiple calculations at once.

  7. #7
    Intelligent DoucheBag jur13n is offline
    MemberRank
    Jan 2008 Join Date
    Zwolle,Location
    1,946Posts

    Re: [Release] [PHP] [OOP] Calculator

    Quote Originally Posted by iGalaxy View Post
    @jur13n I wanted to know about multiple calculations at once.
    simple.
    get the string that has been posted, add the rest.
    recalculate..

  8. #8
    Developer iGalaxy is offline
    MemberRank
    Jul 2013 Join Date
    C:/xampp/htdocsLocation
    488Posts

    Re: [Release] [PHP] [OOP] Calculator

    oh....



Advertisement