


Use PHP to implement simple addition, subtraction, multiplication and division calculator functions
Everyone must have used addition, subtraction, multiplication and division calculators. This article will introduce to you how to use PHP to implement
Calculators based on small programs written in PHP basic language
Requirements: Enter numbers in the input box to perform addition, subtraction, multiplication, and division operations (html+php)
Ideas:
1First create the input numbers and operators In the input box, use the text attribute of input for numbers, and the option attribute of selelct for operators
2. Click the = sign in the input box to perform the corresponding operation,
3 The input box with the = sign can be made using input's submit. Just click the submit form and the content will be passed to php
4 Determine the operator obtained from the html and perform the corresponding operation
5 After the operation is completed, the result must be returned to the form (that is, assigning a value to the form)
The code is as follows:
<?php header("content-type:text/html;charset=utf-8"); session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" dir="ltr"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title>简单计算机</title> </head> <body> <form action="jisuan.php" method="post"> 第一个数<input type="text" value="" name="num1"><br /> 计算符号<select name="oper"> <option value="+">+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> </select><br /> 第二个数<input type="text" value="" name="num2"><br /> <input type="submit" value="计算结果"><br /> </form> </body> </html> <?php $num1=$_POST['num1']; $num2=$_POST['num2']; $oper=$_POST['oper']; $rs=0; switch($oper){ case "+": $rs=$num1+$num2; break; case "-": $rs=$num1-$num2; break; case "*": $rs=$num1*$num2; break; case "/": $rs=$num1/$num2; break; default: echo "您输入的不正确"; } $_SESSION['rs']=$rs; echo '计算结果为:'.$_SESSION['rs']; ?>
The above is the detailed content of Use PHP to implement simple addition, subtraction, multiplication and division calculator functions. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
