Chapter 4 php mathematical operations

Source: Internet
Author: User
Tags echo 7 mathematical functions

I. Numerical 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.
Copy codeThe Code is as follows:
<? Php
$ A = '5 ';
// The string of a number is also a number, which is used in mathematical operations as a number.
Echo is_numeric ($ a); // 1
Echo '<br/> ';
Echo 7 + $ a; // 12
Echo '<br/> ';
Echo '7' + $ a; // 12
Echo '<br/> ';
// Use. After the connection, it will be processed by string
Echo '7'. $ a; // 75
?>

Ii. Random Number
The Rand () function is a simple package for a random function defined in libc.
The Mt_rand () function is a good substitute for implementation.
Copy codeThe Code is as follows:
<? Php
$ A = rand (0, 10 );
Echo $;
Echo '<br/> ';
Echo getrandmax ();
Echo '<br/> ';
$ B = mt_rand (0, 10 );
Echo $ B;
Echo '<br/> ';
Echo mt_getrandmax ();
Echo '<br/> ';
?>

Output
1
32767
6
2147483647
3. format data
Copy codeThe Code is as follows:
<? Php
$ A = 12345.6789;
// Sets the number of decimal places to be retained.
Echo number_format ($ a, 2 );
Echo '<br/> ';
// You can also change the default decimal point and the decimal point.
Echo number_format ($ a, 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.

Copy codeThe Code is as follows:
<? Php
$ A =-123456.789;
$ B = array (1, 2, 3, 4 );
Echo abs ($ );
Echo '<br/> ';
Echo floor ($ );
Echo '<br> ';
Echo ceil ($ );
Echo '<br> ';
Echo round ($ );
Echo '<br> ';
Echo min ($ B );
Echo '<br> ';
Echo max ($ 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.