Home > Backend Development > PHP Tutorial > How to implement simple arithmetic operations in php?

How to implement simple arithmetic operations in php?

WBOY
Release: 2023-06-01 08:44:02
Original
1605 people have browsed it

In the process of web development, arithmetic operations are one of the essential functions. As a widely used server-side scripting language, PHP provides numerous arithmetic operations functions and methods. This article will introduce in detail how to use PHP to implement simple arithmetic operations.

1. Variable assignment

In PHP, use the assignment operator (=) to assign a value to a variable. Variables can store various types of values, such as strings, integers, or floating point numbers. The following is the basic syntax for variable assignment:

<?php
  $a = 10;  //将整数10赋值给变量$a
  $b = 3.14;  //将浮点数3.14赋值给变量$b
  $c = "hello world"; //将字符串"hello world"赋值给变量$c
?>
Copy after login

2. Mathematical operators

PHP supports six basic mathematical operators, namely addition (), subtraction (-), and multiplication (), division (/), modulo (%) and exponent (*). These operators can be used on any type of number, including integers and floating point numbers. Here are examples of each operator:

1, addition operator ( )

<?php
  $a = 10;
  $b = 20;
  $c = $a + $b;
  echo $c;  //输出30
?>
Copy after login

2, subtraction operator (-)

<?php
  $a = 20;
  $b = 10;
  $c = $a - $b;
  echo $c;  //输出10
?>
Copy after login

3, multiplication operator ( *)

<?php
  $a = 10;
  $b = 20;
  $c = $a * $b;
  echo $c;  //输出200
?>
Copy after login

4. Division operator (/)

<?php
  $a = 20;
  $b = 10;
  $c = $a / $b;
  echo $c;  //输出2
?>
Copy after login

5. Modulo operator (%)

The modulo operator returns the result of dividing two numbers remainder.

<?php
  $a = 20;
  $b = 10;
  $c = $a % $b;
  echo $c;  //输出0
?>
Copy after login

6. Exponential operator (**)

The exponent operator returns the exponential power of a base number.

<?php
  $a = 2;
  $b = 3;
  $c = $a ** $b;
  echo $c;  //输出8
?>
Copy after login

3. Auto-increment and auto-decrement operators

PHP also provides auto-increment ( ) and auto-decrement (--) operators, which are used to increase or decrease the value of a variable. The increment and decrement operators can only be used with numeric variables.

1. Auto-increment operator ( )

The auto-increment operator adds 1 to the value of the variable.

<?php
  $a = 10;
  $a++;
  echo $a;  //输出11
?>
Copy after login

2. Decrement operator (--)

The decrement operator decreases the value of the variable by 1.

<?php
  $a = 10;
  $a--;
  echo $a;  //输出9
?>
Copy after login

4. Mathematical functions

PHP also provides some functions for mathematical calculations, such as absolute value, square root, sine and cosine. The following are some commonly used mathematical functions:

1. abs(): Returns the absolute value of a number.

<?php
  $a = -10;
  $b = abs($a);
  echo $b;  //输出10
?>
Copy after login

2. sqrt(): Returns the square root of a number.

<?php
  $a = 16;
  $b = sqrt($a);
  echo $b;  //输出4
?>
Copy after login

3. sin(): Returns the sine value of an angle.

<?php
  $a = 30;
  $b = sin($a * pi() / 180);  //将角度转换为弧度
  echo $b;  //输出0.5
?>
Copy after login

4. cos(): Returns the cosine value of an angle.

<?php
  $a = 60;
  $b = cos($a * pi() / 180);  //将角度转换为弧度
  echo $b;  //输出0.5
?>
Copy after login

5. tan(): Returns the tangent value of an angle.

<?php
  $a = 45;
  $b = tan($a * pi() / 180);  //将角度转换为弧度
  echo $b;  //输出1
?>
Copy after login

5. Summary

In PHP, you can use basic operators and functions to perform arithmetic operations. Arithmetic operations can be easily implemented using mathematical operators and functions, while the values ​​of variables can be easily modified using increment and decrement operators. More advanced mathematical functions can also be used when more complex mathematical calculations are required. I hope this article can help everyone learn and use PHP.

The above is the detailed content of How to implement simple arithmetic operations in php?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template