Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Top Posters

Who's Online (1)

Powered by Vanilla. Made with Bootstrap.
A simple Calculator Example Using OOP
  • A simple code for a calculator using OOP approach is given below..;)..


    <html>
    <head>
    <h1 align='center'>Its a simple calculator Program using OOP approach. </h1>
    <title> Calculator</title>
    </head>
    <body>
    <?php                  //starting the php code
    class calc {           //declaring a class with name calc
    private $txt3;         //declaring a private property for class calc, which can be access with in the class and hide from outside world.
    public function setdata($txt1,$txt2) {    //declaring a method to set the data for the property.
    if($txt2=='0')                            //To check if the value of second number is zero or not. If it is zero then print the error message.
    {
    echo "Error:-Second number must be a digit except a 0 !!!";
    $this->txt3='Error';
    }
    elseif($_POST['cal']=='add')        //To check if add is selected or not when submitting a form. If it is selected go with in the if and do addition.
    {
    $this->txt3=$txt1+$txt2;            //Use of this variable to refer txt3 to itself.
    }
    elseif($_POST['cal']=='sub')        //To check if sub is selected or not when submitting a form. If it is selected go with in the if and do substraction.
    {
    $this->txt3=$txt1-$txt2;
    }
    elseif($_POST['cal']=='mul')    //To check if mul is selected or not when submitting a form. If it is selected go with in the if and do multipliction.
    {
    $this->txt3=$txt1*$txt2;
    }
    else                            //this is the else statement for the divide.
    $this->txt3=$txt1/$txt2;
    }

    public function getdata()        //declaring the method to show the output.
    {
    echo "<p align='center'><font color='red' size='5px'><u><b><i>Result is:  $this->txt3</i></b></u></font></p>";
    }
    }
    $obj1=new calc();            //creating an object for class calc to provide service to the client.
    $obj1->setdata($_POST['txt1'],$_POST['txt2']);  //calling the object method to set the data with arguments passed by the user using form.
    $obj1->getdata();                        // calling the object method to get the output.
    ?>
    <form action='test.php' method='post'>
    Number 1:-<input type='text' name='txt1' /> <br /><br />
    Number 2:-<input type='text' name='txt2' /><br /><br />
    <input type='radio' name='cal' value='add' >add</input>
    <input type='radio' name='cal' value='sub' >sub</input>
    <input type='radio' name='cal' value='mul' >mul</input>
    <input type='radio' name='cal' value='div' >div</input><br/><br/>
    <input type='submit' name='submit' value='submit'/>
    </form>

    </body>
    </html>


    All of the code is explained before or using the comments in the code. Thus I am not going to explain it again...;).. Thank you..:).. Hope this will help you guys..:)