Simple Calculator Program in PHP — Tutorials Class

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.

Читайте также:  Checking java heap space

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

First Number

Second Number

Result

simple-calculator-program-in-PHP-using-switch-case

Share this post:

Tutorials
PHP Decision Making
Exercises & Assignments
Write a program to check student grade based on marks
Write a program to show day of the week using switch
Write a program to calculate Electricity bill in PHP
Write a simple calculator program in PHP using switch case
Write a PHP program to check if a person is eligible to vote
Write a PHP program to check whether a number is positive, negative or zero
Interview Questions & Answers
No Content Found.
  • PHP Exercises Categories
  • PHP Functions Categories
  • PHP Loops Categories
  • PHP MySQL Categories
  • PHP String Categories
  • PHP Syntax Categories
  • PHP Variables Categories
  • PHP All Exercises & Assignments Categories
  • PHP Array Categories
  • PHP Decision Making Categories
  • PHP Top Exercises Categories
  • PHP Operators Categories
  • All Tutorials
  • PHP Tutorial
  • HTML Tutorial
  • SEO Tutorial
  • C Tutorial
  • CSS Tutorial
  • WordPress Tutorial
  • Python Tutorial
  • PHP REST API
  • jQuery Tutorial
  • JavaScript Tutorial
  • Bootstrap Tutorial

Tutorials Class - Online Tutorials, Exercises, & Interview Questions

Training Partner

Merient Infotech - Training Courses in Rohtak

About Tutorials Class

Tutorials Class (TutorialsClass.com) is one stop portal to learn online about different web technologies, preparing for an interview and enhancing your technical skills.

We provide free online tutorials on the latest web technologies. These tutorials are well structured and easy to use for beginners. With each tutorial, you may find a list of related exercises, assignments, codes, articles & interview questions.

This website provides tutorials on PHP, HTML, CSS, SEO, C, C++, JavaScript, WordPress, and Digital Marketing for Beginners. Start Learning Now.

Источник

Simple Calculator Using PHP

This is a simple calculator that can perform the simple arithmetic tasks (+, -, * and /).

Write a simple calculator program in PHP using if-else statement

HTML Code for Simple Calculator

PHP Code for Simple Calculator

We are using Else-If statement for performing the functions.

Download Code – Simplecalculator

Write a simple calculator program in PHP using the switch statement

Operations on calculator

Following operations will perform on the given calculator;

Calculator in PHP using function

PHP calculator using class

Write a PHP program to make a calculator class that will accept two values as a parameter, then perform the operation of addition, multiplication, subtraction, and division.

More Examples of Calculators

  1. PHP Calculator using GET method
  2. PHP mortgage calculator script
  3. Age calculator in PHP
  4. GPA calculator in PHP
  5. Scientific calculator in PHP with database
  6. Interest, Saving or Profit Calculator in PHP
  7. PHP grade calculator code
  8. PHP Calculator using Switch statement
  9. PHP Calculator with Class and Objects
  10. Calculator design in CSS and Javascript

Topic Covered:

Calculator in PHP using the button, PHP calculator code, how to make a calculator in PHP, PHP code for a calculator with buttons, PHP calculator form, calculator in PHP and HTML, PHP calculator script, create a calculator in PHP, PHP calculator source code download.

Источник

Оцените статью