Chapter 4 mathematical operations

Source: Internet
Author: User

I.Numeric data type

Numeric or numeric data are generally two types of double and INT in PHP.

PHP is a loose scripting language. Pay attention to the type conversion method.

<? PHP

$='5';

//The string of a number is also a number. It is used in mathematical operations as a number.

EchoIs_numeric ($);// 1

Echo'<Br/>';

Echo7+$;// 12

Echo'<Br/>';

Echo'7'+$;// 12

Echo'<Br/>';

//Use.After connection, it will be processed by string

Echo'7'.$;// 75

?>

 

II.Random Number

Rand ()A function is a simple package of a random function defined in libc.

Mt_rand ()Function is a good alternative to implementation.

<? PHP

$= Rand (0,10);

Echo$;

Echo'<Br/>';

EchoGetrandmax ();

Echo'<Br/>';

 

$ B= Mt_rand (0,10);

Echo$ B;

Echo'<Br/>';

EchoMt_getrandmax ();

Echo'<Br/>';

?>

Output

1
32767
6
2147483647

 

3..Format data

<? PHP

$=12345.6789;

//Used to set the number of decimal points retained

EchoNumber_format ($,2);

Echo'<Br/>';

//You can also change the default decimal point and kilobytes.

EchoNumber_format ($,2,'#','*')

?>

Output

12,345.68
12*345 #68

 

Iv. mathematical functions

Function

Function

ABS ()

Take absolute value

Floor ()

Rounding

Ceil ()

Returns an integer.

Round ()

Rounding

Min ()

Minimum value or the minimum value in the array

Max ()

Returns the maximum value or the maximum value in the array.

 

<? PHP

$=-123456.789;

$ B=Array(1,2,3,4);

EchoABS ($);

Echo'<Br/>';

EchoFloor ($);

Echo'<Br>';

EchoCeil ($);

Echo'<Br>';

EchoRound ($);

Echo'<Br>';

EchoMin ($ B);

Echo'<Br>';

EchoMax ($ B);

?>

Output

123456.789
-123457
-123456
-123457
1
4

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.