第四章 php数学运算_PHP
一.数值数据类型
数字或数值数据在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

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



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 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.

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.

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 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)

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

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.

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
