Showing posts with label php object oriented programming sum of numbers. Show all posts
Showing posts with label php object oriented programming sum of numbers. Show all posts

Tuesday, December 9, 2014

Addition of Numbers in PHP Using OOP

As I try to improve my programming skills in PHP I modify my previous code and converted it into Object Oriented Programming approach. This program is very simple application that will ask the user to enter three numbers and find the total sum of the three numbers. I will give the user a chance to clear the text box using the clear button.

The things that you can learn in this sample program it will introduce you will the concept of object oriented programming by creating a class, creating an object and using the attributes like the variables we declare and the use of behavior or method to carry out the addition of the three numbers. I use Twitter Bootstrap CSS framework to improve the appearance of the web page. I make this program very simple to understand and use for beginners that are new in PHP programming and object oriented approach in PHP.

If you have some questions about my work feel free to send me an email at jakerpomperada@yahoo.com and jakerpomperada@gmail.com. I will be happy to read all your emails and suggestions how I can improve my works in programming.

People here in the Philippines who wish to contact me can reach me at my mobile numbers : 09173084360 and 09993969756.

Thank you very much and Happy Productive Programming.



Sample Output of Our Program

Program Listing

header.php

<head>
<title> Addition of Numbers in OOP </title>
<link rel="stylesheet" href="css/bootstrap.min.css" />
<style type="text/css">
#wrapper {
margin: 0 auto;
float: none;
width:70%;
}
.header {
padding:10px 0;
}
.title {
padding: 0 5px 0 0;
float:left;
margin:0;
}
.container form input {
height: 30px;
}
</style>
</head>
<body>
<div id="wrapper">
<div class="header">
<h3 class="title"> Addition of Numbers Using OOP</h3>
<br>
</div>

index.php

<html>
<body>
<?php include 'template-parts/header.php' ?><br>
<?php 
class find_sum {
    private $val1 , $val2, $val3;
    public function __construct($val_1, $val_2, $val_3){
        $this->val1 = $val_1;
        $this->val2 = $val_2;
$this->val3 = $val_3;
    }
    public function add(){
       return $this->val1 + $this->val2 + $this->val3;
    }
}
?>
<div class="container home">
<?php
    $value1 = $_POST['num1'];
    $value2 = $_POST['num2'];
$value3 = $_POST['num3'];
    if(isset($_POST['solve']))
    {
        $add_calc = new find_sum($value1,$value2,$value3);    
$result = "The total sum of $value1, $value2 
         and $value3 is ".$add_calc->add().".";
    }
if (isset($_POST['clear']))
{
$value1=""; $value2=""; $value3="";
$holder=""; $result="";
}
?>
<form action="index.php" method="post">
<font size="4">
<label> Enter First Number: </label>
<input type="text" placeholder="first number"  name="num1" value="<?php echo $value1; ?>" />
<br />
<label> Enter Second Number: </label>
<input type="text" placeholder="second number"  name="num2" value="<?php echo $value2; ?>"  />
<br />
<label> Enter Third Number: </label>
<input type="text" placeholder="third number"  name="num3" value="<?php echo $value3; ?>"  />
<br /> </font>
<input type="submit" name="solve" value="Compute"
 title="Click here to find the total sum." class="btn btn-info">
<input type="submit" name="clear" value="Clear"
 title="Click here to clear text box." class="btn btn-info">
</form>
<?php
echo "<br>";
echo "<font size='5'>";
echo $result;
echo "</font>";
?>
</div>
</body>
</html>