PHP Calculator in OOP
![]() |
Object Oriented PHP(php oop) Part-02 (php calculator in oop) |
In this lesson we are going to learn about how to create calculator in php oop.
it's will help you to understand php class,property and object.
create 2 files named:
1.index.php
2.function.php
Step 1: Codes for Index.php
HTML codes top of the page<html> <head> <title>Working with Method and Object in PHP OOP</title> </head> <form method="POST" action=""> <span>Enter First Number:</span><br/> <input type="number" name="num1" required/><br > <span>Enter Seconds Number</span><br > <input type="number" name="num2" required/><br > <input type="submit" name="submit" value="Calculation"/> </form> </html>
Add the below php codes end of the html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?php include ( "function.php" ); if (isset( $_POST [ "submit" ])){ $numOne = $_POST [ 'num1' ]; $numTwo = $_POST [ 'num2' ]; if ( empty ( $numOne ) or empty ( $numTwo )){ echo "Fields are Mandatory" ; } else { echo "Your First Number is: $numOne Seconds Number is: $numTwo "; //Calling Class Calculator $obj = new Calculator; // $obj ->add( $numOne , $numTwo ); $obj ->sub( $numOne , $numTwo ); $obj ->mul( $numOne , $numTwo ); $obj ->div( $numOne , $numTwo ); } } ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?php class Calculator{ public function add( $a , $b ){ echo 'Summation: ' .( $a + $b ). '<br/ > ' ; } public function sub( $a , $b ){ echo 'Subtraction: ' .( $a - $b ). '<br/ > ' ; } public function mul( $a , $b ){ echo 'Multiplication: ' .( $a * $b ). '<br/ > ' ; } public function div( $a , $b ){ echo 'Division: ' .( $a / $b ); } } ?> |
![]() |
Object Oriented PHP(php oop) Part-02 (php calculator in oop) |
No comments:
Post a Comment
Thank You for Your Comment
Note: Only a member of this blog may post a comment.