Huwebes, Marso 3

PHP: Simple Calculator


Here are the steps in creating a simple calculator using PHP.
You can use either Switch or If Statement.





Step 1: Copy the following HTML code into a notepad or dreamweaver.
<html>
<head><title>PHP Simple Calculator</title></head>
<body>
<form method="post">
Enter First No.
<input type="text" name="firstnum">
Select operator:
<select name="operator">
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
</select>
Enter Second No.
<input type="text" name="secondnum">
<input type="submit" name="calculate" value="Calculate">
</form>
</body>
</html>
Step 2: Add the following PHP script anywhere in the page where you want the result will be displayed.
<?php
$first=$_POST['firstnum'];
$second=$_POST['secondnum'];
$operator=$_POST['operator'];
if ($operator=="+")
{$total=$first + $second;}
if ($operator=="-")
{$total=$first - $second;}
if ($operator=="*")
{$total=$first * $second;}
if ($operator=="/")
{$total=$first / $second;}
echo $total;
?>

Step 3: Save you page with the .php file extension in the directory wamp/www. Be sure to have any mini-server such as Wampserver or Easy PHP installed. If you don't have any, here's a link to download it. wampserver or easyphp

0 comments:

Mag-post ng isang Komento

 
Powered by Blogger