Home Backend Development PHP Tutorial 第四章 php数学运算_PHP

第四章 php数学运算_PHP

Jun 01, 2016 pm 12:14 PM
computation

一.数值数据类型
数字或数值数据在PHP中一般就两种double和int。
PHP是一种松散类型的脚本语言,要注意类型转换的方式。
复制代码 代码如下:
$a = '5';
//数字的字符串也是数字,参与数学运算当数字处理
echo is_numeric ( $a ); //1
echo '
';
echo 7 + $a; //12
echo '
';
echo '7' + $a; //12
echo '
';
//用.连接后就按字符串处理
echo '7' . $a; //75
?>

二.随机数
Rand()函数是libc中定义的一个随机函数的简单包装器。
Mt_rand()函数是一个很好的代替实现。
复制代码 代码如下:
$a = rand(0,10);
echo $a;
echo '
';
echo getrandmax();
echo '
';
$b = mt_rand(0,10);
echo $b;
echo '
';
echo mt_getrandmax();
echo '
';
?>

output
1
32767
6
2147483647
三.格式化数据
复制代码 代码如下:
$a = 12345.6789;
//用于设置保留多少位小数点
echo number_format($a,2);
echo '
';
//也可以改变默认小数点的符号表示和千分位的表示符号
echo number_format($a,2,'#','*')
?>

Output
12,345.68
12*345#68
四.数学函数

函数

功能

Abs()

取绝对值

Floor()

舍去法取整

Ceil()

进一法取整

Round()

四舍五入

Min()

求最小值或数组中最小值

Max()

求最大值或数组中最大值

复制代码 代码如下:
$a = -123456.789;
$b = array (1, 2, 3, 4 );
echo abs ( $a );
echo '
';
echo floor ( $a );
echo '
';
echo ceil ( $a );
echo '
';
echo round ( $a );
echo '
';
echo min ( $b );
echo '
';
echo max ( $b );
?>

output
123456.789
-123457
-123456
-123457
1
4
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Learn how to calculate variance in Go Learn how to calculate variance in Go Feb 23, 2024 pm 09:30 PM

Learn how to solve for variance in Golang. In statistics, variance is an important indicator of the dispersion of a set of data. It is used to measure the difference between each data point in the data set and the mean. In Golang, we can solve for the variance of a set of data by writing code. Next, we will introduce how to implement variance calculation in Golang and provide specific code examples. 1. Definition of variance The calculation formula of variance is as follows: [Var(X)=rac{

How to use the math module to perform mathematical operations in Python 3.x How to use the math module to perform mathematical operations in Python 3.x Aug 01, 2023 pm 03:15 PM

How to use the math module to perform mathematical operations in Python 3.x Introduction: In Python programming, performing mathematical operations is a common requirement. In order to facilitate processing of mathematical operations, Python provides the math library, which contains many functions and constants for mathematical calculations and mathematical functions. This article will introduce how to use the math module to perform common mathematical operations and provide corresponding code examples. 1. Basic mathematical operation addition is performed using the function math.add() in the math module.

How does the C++ library perform mathematical calculations? How does the C++ library perform mathematical calculations? Apr 18, 2024 pm 10:21 PM

The header files in the C++ standard library provide a wealth of mathematical functions, including trigonometric functions, hyperbolic functions, exponential and logarithmic functions, etc. These functions make it easy to perform common mathematical operations such as calculating the area of ​​a circle, the Pythagorean Theorem, solving quadratic equations, and finding extreme values.

How to solve Python math operation errors? How to solve Python math operation errors? Jun 24, 2023 pm 03:39 PM

Python is a high-level programming language with rich support for numerical calculations. Python has a large number of built-in mathematical functions and operators to make mathematical operations easier. However, even for experienced Python developers, there are situations where errors can occur when working with mathematical operations. This article will introduce how to solve mathematical operation errors in Python. 1. Dealing with floating-point number accuracy issues. The accuracy of Python’s built-in floating-point number operations is limited. Therefore, when performing floating-point number operations, sometimes

How to use exponential functions to perform mathematical operations in C language How to use exponential functions to perform mathematical operations in C language Feb 22, 2024 pm 06:42 PM

How to use exponential functions to perform mathematical operations in C language 1. Introduction The exponential function is one of the commonly used functions in mathematics and can be used to calculate exponentials, logarithms, power operations, etc. In C language, we can use the exponential function library provided in the math.h header file to perform mathematical operations. This article will introduce how to use exponential functions to perform mathematical operations in C language and provide specific code examples. 2. Introduction to the exponential function The exponential function e^x (also called the natural exponential function) is an exponential function with the natural constant e as the base, expressed as exp(x)

Revealing the PHP BCMath extension: digital magic under precision control Revealing the PHP BCMath extension: digital magic under precision control Feb 23, 2024 am 09:34 AM

Introduction to BCMath extension BCMath extension is a set of function libraries for arbitrary precision mathematical operations. It allows you to perform addition, subtraction, multiplication, division, remainder, exponentiation, and more without worrying about numeric overflow or rounding errors. The BCMath extension uses Binary Coded Decimal (BCD) to store numbers. BCD is an encoding that represents decimal numbers as binary numbers. This encoding method can avoid numerical overflow and rounding errors, thereby ensuring the accuracy of calculation results. The BCMath extension provides a series of functions to perform arbitrary precision mathematical operations. These functions include: bcadd(): addition operation bcsub(): subtraction operation bcmul(): multiplication operation bcdiv(): division operation bcmo

How to perform mathematical operations using PHP built-in functions? How to perform mathematical operations using PHP built-in functions? Apr 22, 2024 pm 02:42 PM

PHP's built-in mathematical functions can perform various operations, including basic arithmetic operations (+, -, *, /, %), rounding, integer, absolute value, maximum value, minimum value, power, square root. In practical cases, you can calculate the area of ​​a circle, the average of two numbers, and determine whether a number is even.

Detailed explanation of PHP mathematical functions Detailed explanation of PHP mathematical functions Jun 20, 2023 am 09:49 AM

As a widely used back-end language, PHP provides many functions for mathematical calculations. This article will delve into PHP's mathematical functions, their usage and functions to help readers better understand and apply these functions. abs - Take absolute value The abs() function is used to return the absolute value of the argument passed to it. It accepts an argument of type integer or floating point number and returns its absolute value. For example, abs(-5) will return 5 and abs(2.4) will return 2.4. ceil - round up cei

See all articles