Showing posts with label Math Calculator in PHP. Show all posts
Showing posts with label Math Calculator in PHP. Show all posts

Friday, May 30, 2014

Simple Calculator in PHP

This simple program in PHP will allow the user to perform basic mathematical operations using PHP as your web scripting programming language it a simple calculator program in PHP. Before we can accept values from the use first we make a web form layout that will accept input values from our user. Their are two text field in HTML the first text field will ask the user to enter the first value and the second text field will ask also the user to enter the first value.

After which our program will give the user to select mathematical operations using the combo box in our web page their are for operations the addtion, subtraction, multiplication and division.If the user select addition the user can click the compute button which will perform the computation and display the results on the web page in the text field. 

This simple calculator program that I wrote is intended for beginners that are new in PHP programming. Based in my experience as a software developer and web developer in learning PHP you must have basic understanding and knowledge in HTML because HTML is the one will accept input values from the user of your program. It will follow the basic understanding how the syntax and commands in PHP making small programming will develop your skills and logic how the program works.

I hope you will find my work useful in your quest in learning PHP as your web scripting programming language.

Thank you very much until the next article.



Sample Output of our Program

Program Listing


<?php
  // Written By: Jake R. Pomperada
  // May 30, 2014 Friday
  // Tools : Wamp Server
  // Email Address: jakerpomperada@yahoo.com
  
  error_reporting(0);
  if ($_REQUEST['clear'])
   {
    
$val_1 ="";
$val_2 ="";
$result ="";
}

   
  if ($_REQUEST['submit']) 
     {
   
$choice = $_REQUEST['choice'];  
 if ($choice == "1")
   {
$val_1 = $_REQUEST['val_1'];
$val_2 = $_REQUEST['val_2'];
$result = $_REQUEST['val_1'] +  $_REQUEST['val_2'];

}
elseif ($choice == "2")
   {
    $val_1 = $_REQUEST['val_1'];
$val_2 = $_REQUEST['val_2'];
$result = $_REQUEST['val_1'] -  $_REQUEST['val_2'];

}
elseif ($choice == "3")
   {
$val_1 = $_REQUEST['val_1'];
$val_2 = $_REQUEST['val_2'];
$result = $_REQUEST['val_1'] *  $_REQUEST['val_2'];
}
elseif ($choice == "4")
   {
$val_1 = $_REQUEST['val_1'];
$val_2 = $_REQUEST['val_2'];
    $result = $_REQUEST['val_1'] /  $_REQUEST['val_2'];
 
  }

?>  
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<div align="center">
  <p><font size="4"><strong>Calculator 1.0</strong></font></p>

<form name="form2" method="post" action="">
    <p align="left">Enter First Number 
      <input type="text" name="val_1" 
 value="<?php echo $val_1; ?>">
  </p>
    <div align="left">Enter Second Number 
      <input type="text" name="val_2" 
 value="<?php echo $val_2; ?>">
 
    </div>

    <p align="left"><strong>Math Operation</strong></p>
    <p align="left"><strong>
    <select name="choice">
<option value="1"> Addition</option>
<option value="2"> Subtration</option>
  <option value="3"> Multiplication</option>
  <option value="4"> Division</option>
</select>
      </strong></p>
     
    <div align="left"><strong>Result</strong> 
      <input type="text" name="result" 
  value="<?php echo $result ?>" readonly>
    </div>
   <input type="submit" name="submit" value="Compute">
    <tr> </tr>  <tr> </tr> 
   <input type="submit" name="clear" value="Clear">

  </form>
 <p>&nbsp;</p>
</div>
</body>
</html>