- Calculator in PHP using If-Else Statement
- jkuip / calculator.php
- Write a simple calculator program in PHP using switch case
- Share this post:
- Training Partner
- About Tutorials Class
- Popular Tutorials
- Simple Calculator Using PHP
- Write a simple calculator program in PHP using if-else statement
- HTML Code for Simple Calculator
- PHP Code for Simple Calculator
- Download Code – Simplecalculator
- Write a simple calculator program in PHP using the switch statement
- Operations on calculator
- Calculator in PHP using function
- PHP calculator using class
- More Examples of Calculators
- Related Posts:
Calculator in PHP using If-Else Statement
PHP (Hypertext Preprocessor) is an open-source & server-side scripting language designed specifically for web development, where the scripts are executed on the server. It is free to download and use & can be easily embedded with HTML files. We can also write HTML codes in a PHP file. PHP calculator is used for performing basic mathematical operations like Addition, subtraction, multiplication, and division. In this article, we will see how to design the Calculator In PHP to perform basic arithmetic operations. For this, we will be using PHP, HTML, & JavaScript.
A simple calculator can be made using a PHP program that is able to add, subtract, multiply and divide, two operands entered by the user. The if-else statement will be used to create a calculator.
Approach: The following approach will be used to create the basic calculator:
We will declare two local variables say num1 and num2 for two numeric values. Then, we will enter the choice(Addition, subtraction, multiplication, or division). Then, it takes two numbers, num1, and num2 for further calculation. With the help of the if-else statement, it will jump to an operator selected by the user. Using the HTML table created the design where variables are holding the input field and the rest of them are filled with input buttons. Now, with the help of function res(), it displays the respective value of the button to the input field every time we click the button. The ans() function is used to set the value pressed from the keyboard to the same input field. All the calculations of the numbers entered can be done by both the “Enter” key and also with the help of the “=” button. The calculate() function will do the calculation with math.evaluate() function and then displays the final result to the input field. The clear_input() function is used whose work is to clear the input field.
Example: This example illustrates the PHP Calculator by implementing the basic if-else statement.
jkuip / calculator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
< title >Calculator |
< link href =" https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css " rel =" stylesheet " > |
// If the submit button has been pressed |
if (isset( $ _POST [ ‘submit’ ])) |
// Check number values |
if (is_numeric( $ _POST [ ‘number1’ ]) && is_numeric( $ _POST [ ‘number2’ ])) |
// Calculate total |
if ( $ _POST [ ‘operation’ ] == ‘plus’ ) |
$ total = $ _POST [ ‘number1’ ] + $ _POST [ ‘number2’ ]; |
> |
if ( $ _POST [ ‘operation’ ] == ‘minus’ ) |
$ total = $ _POST [ ‘number1’ ] — $ _POST [ ‘number2’ ]; |
> |
if ( $ _POST [ ‘operation’ ] == ‘times’ ) |
$ total = $ _POST [ ‘number1’ ] * $ _POST [ ‘number2’ ]; |
> |
if ( $ _POST [ ‘operation’ ] == ‘divided by’ ) |
$ total = $ _POST [ ‘number1’ ] / $ _POST [ ‘number2’ ]; |
> |
// Print total to the browser |
echo » < $ _POST [ 'number1' ]> < $ _POST [ 'operation' ]> < $ _POST [ 'number2' ]>equals «; |
> else |
// Print error message to the browser |
echo ‘Numeric values are required’ ; |
> |
> |
?> |
< input name =" number1 " type =" text " class =" form-control " style =" width: 150px; display: inline "/> |
< option value =" plus " >Plus |
< option value =" minus " >Minus |
< option value =" times " >Times |
< option value =" divided by " >Divided By |
< input name =" number2 " type =" text " class =" form-control " style =" width: 150px; display: inline "/> |
< input name =" submit " type =" submit " value =" Calculate " class =" btn btn-primary "/> |
Write a simple calculator program in PHP using switch case
You need to write a simple calculator program in PHP using switch case.
View Solution/Program
> ?> PHP - Simple Calculator Program