Saturday, 5 July 2014

Create Simple Calculator in PHP

Create Simple Calculator in PHP



simple-caculator-in-php.gif

So, in this application I am using two input HTML TextBoxes for entering operands for calculations, a button for the calculation and a drop down list for selecting an operator, and a button for the calculation. In it is simply a HTML TextBox value, in other words operand value and also an operator value is accepted in this way:
$_REQUEST['name']
In that way you can simply store the TextBox value or drop down list value for calculation in a PHP variable. I handle lost of errors in this script. I am simply checking that the TextBox value is empty or not using:

if($_REQUEST['
fvalue']==NULL && $_REQUEST['lvalue']==NULL)
If an error exists then an alert is shown with a message depending on the error.
 
The following is the calculator code:
<?php
ini_set('display_errors',0);
ifisset( $_REQUEST['calculate'] ))
{
$operator=$_REQUEST['operator'];
if($operator=="+")
{
$add1 = $_REQUEST['fvalue'];
$add2 = $_REQUEST['lvalue'];
$res= $add1+$add2;
}
if($operator=="-")
{
$add1 = $_REQUEST['fvalue'];
$add2 = $_REQUEST['lvalue'];
$res= $add1-$add2;
}
if($operator=="*")
{
$add1 = $_REQUEST['fvalue'];
$add2 = $_REQUEST['lvalue'];
$res =$add1*$add2;
}
if($operator=="/")
{
$add1 = $_REQUEST['fvalue'];
$add2 = $_REQUEST['lvalue'];
$res= $add1/$add2;
}
if($_REQUEST['fvalue']==NULL && $_REQUEST['lvalue']==NULL)
{
echo "<script language=javascript> alert(\"Please Enter values.\");</script>";
}
else if($_REQUEST['fvalue']==NULL)
{
echo "<script language=javascript> alert(\"Please Enter First value.\");</script>";
}
else if($_REQUEST['lvalue']==NULL)
{
echo "<script language=javascript> alert(\"Please Enter second value.\");</script>";
}
}
?>
<form>
<table style="border:groove #00FF99">
            <tr>
                <td style="background-color:aquacolor:redfont-family:'Times New Roman'">Enter First Number</td>
                <td colspan="1">
               
                    <input name="fvalue" type="text" style="color:red"/></td>
            <tr>
                <td style="color:burlywoodfont-family:'Times New Roman'">Select Operator</td>
                <td>
                    <select name="operator" style="width63px">
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
</select></td>
               </tr>
            <tr>
                <td style="background-color:aquacolor:redfont-family:'Times New Roman'">Enter First Number</td>
                <td class="auto-style5">
                    <input name="lvalue" type="text"  style="color:red"/></td>
               
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" name="calculate" value="Calculate" style="color:wheat;background-color:rosybrown" /></td>
               
            </tr>
            <tr>
                <td style="background-color:aqua;color:red">Output = </td>
                <td style="color:darkblue"><?php echo $res;?></td>
               
            </tr>
       </table>
 </form>

Output

math-calculator-in-php.gif

What is the Difference Between get , post and request method ?

What is the Difference Between get , post and request method ?

21DEC
In HTML to transfer records from one page to another page , attribute method is used in form tag.
i.e :
<form method="post" action="action.php">
data goes here .......
</form>
Now , In this case action.php is destination page where user will have records .
in PHP : there are 3 methods to retrieve data from receiving page.
1 ) $_GET
2) $_POST
3) R_REQUEST
$_GET :
$_GET is a super global array which is an inbuilt array. Which collects values from a form sent with method=”get” as well it collects values from URL alsoe.g. http://www.domainname.com/pagename.php?data=123)
You can print $_GET with inbuilt function in php i.e ( print_r($_GET) ).
IMP : Information sent from this method will be visible in ( address bar of any browser ) and has limitation on characters .
As per W3C Community limitation can be extended up to 4000 characters
Myth: Search services will not index anything with a “?” in the URI.
Myth: URIs cannot be longer than 256 characters
for details : visit : About $_GET limitation :
You can read more details about $_GET.
$_POST :
$_POST is a super global array which is an inbuilt array. Which collects values from a form sent with method=”post”.
You can print $_GET with inbuilt function in php i.e ( print_r($_POST)  ).
IMP : Information received from POST method is always invisible in ( address bar of any browser ) . It has more limit on sending characters to action page.
This limitation can be set in php.ini file
Note: We can change by setting the POST_MAX_SIZE in the php.ini file . by default we will find this value with 8MB in most of the php.ini file.
$_REQUEST :
$_REQUEST is a super global array which is an inbuilt array. Which collects values from a form sent with  either method=”post” or method=”get” .
You can print $_REQUEST with inbuilt function in php i.e ( print_r($_REQUEST)  ).
IMP : One of the best way of getting data on action page , here we did not require to check that which method we have used to pass data from sending page.Even $_REQUEST get records from the COOKIE also.

Featured post

Life Infotech now a leading brand in the field of technology training

  Life Infotech now a leading brand in the field of technology training & its invites students around the nation to be a part of the Tra...