Create Basic PHP Calculator

Create a file name: calculator.php, then copy to C:\wamp\www\, then go to browser run: http://localhost/calculator.php.

Here is the content of “calculator.php”:

<html>

<head>

<title>Calculator: SEREY Vichhaiy</title>

</head>

 

<body style=”margin-top:50px; background-color:#CCC; “>

<center>

<div style=” width:200px; height:auto; border:1px blue; background-color:#FFF; “>

<p style=” color:#666; font-size:20px; font-family:’Palatino Linotype’; “>Basic PHP Calculator:</p>

<?php

 

if(isset($_POST[‘submit’]))

{

     $firstnumber= $_POST[‘firstnumber’];

     $secondnumber = $_POST[‘secondnumber’];

if(isset($_POST[‘sign’]))

{

 

$sign = $_POST[‘sign’];

if($sign == ”){

         echo ‘<b style=”color:red;”>Require Information Needed!!!</b>’;

 

     }

     elseif($sign==”+”){

         $total = $firstnumber+$secondnumber;

         echo $total;

     }

    
 

     elseif($sign==”-“){

         $total = $firstnumber-$secondnumber;

         echo $total;

 

     }

     elseif($sign==”*”){

         $total = $firstnumber*$secondnumber;

         echo $total;

 

     }

     elseif($sign==”/”){

         $total = $firstnumber/$secondnumber;

         echo $total;

 

     }

     elseif($sign==”%”){

         $total = $firstnumber%$secondnumber;

         echo $total;

 

     }

    }

    

    else{

         echo ‘<b style=”color:red;”>Require Information Needed!!!</b>’;

        }

}

?>

 

<form action=”calculator.php” method=”post”>

<table>

<tr>

<td><input placeholder=”Enter First Number” type=”text” name=”firstnumber” /></td>

</tr>

<tr>

<td>

    <input type=”radio” name=”sign” value=”+” />+

    <input type=”radio” name=”sign” value=”-” />-

    <input type=”radio” name=”sign” value=”*” />*

    <input type=”radio” name=”sign” value=”/” />/

        <input type=”radio” name=”sign” value=”%” />%

</td>

</tr>

<tr>

<td><input placeholder=”Enter Second Number” type=”text” name=”secondnumber” /></td>

</tr>

<tr>

<td><input style=” background-color:#0CF; width:70px; height:25px;” type=”submit” name=”submit” value=”SUBMIT” /></td>

</tr>

</table>

 

</form>

</div>

</center>

</body>

</html>

By vichhaiy Posted in WEP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s