Home >Backend Development >PHP Tutorial >Example analysis of PHP mathematical operations and data processing methods

Example analysis of PHP mathematical operations and data processing methods

墨辰丷
墨辰丷Original
2018-06-04 10:48:151919browse

This article mainly introduces PHP mathematical operations and data processing methods, and analyzes PHP's data types and basic mathematical operation methods in the form of examples. Friends in need can refer to

1. Numeric data type

In PHP, the use of numeric or numerical data and mathematical functions is very simple. Basically, there are two data types to deal with: floating point numbers and integers. The internal representation of floating point and integer values ​​are the C data types double and int respectively. Similar to C, these data types in PHP follow the same set of rules.

PHP is a loosely typed scripting language, and variables can change data types according to calculation needs. This allows the engine to perform type conversions dynamically. So, if a calculation contains a numeric value and a string, the string is converted to a numeric value before the calculation is completed, and the numeric value is converted to a string before being concatenated with the string.

<?php
$a = &#39;5&#39;;
$b = 7 + $a;
echo "7 + $a = $b";
?>

PHP provides a large number of functions to check the data type of variables. There are 3 functions that check whether a variable contains a numeric value, or more specifically, whether a variable is a float or an integer.
The function is_numeric() can check whether the value passed in as a parameter is a numeric value.

The functions is_int() and is_float() are used to check specific data types. If an integer or floating point number is passed in, these functions will return true, otherwise they will return false, even if a string with a legal numerical representation is passed in, they will return false.

You can also force the engine to change the data type. This is called type casting, which can be achieved by adding (int), (integer), (float), (double) or (real) in front of the variable or value, or by using the function intval() or floatval() .

two. Random numbers

Random numbers are a science in themselves. There have been many different implementations of random number generators. PHP implements two of them: rand() and mt_rand(). The rand() function is a simple wrapper around a random function defined in libc, one of the basic libraries provided by the compiler used to build PHP. mt_rand() is a good alternative implementation that provides many well-designed features, and mt_rand() is even faster than the version in libc.

Both functions provide some functions to get the value of MAX_RAND. rand() is getrandmax(), mt_rand() is mt_getrandmax();

3. Formatted data

In addition to warnings, errors and other information, most of PHP's output is generated using functions such as echo, print() and printf(). These functions convert the argument into a string and send it to the client application.
The number_format() function can convert integers and floating-point values ​​into a readable string representation.

<?php 
$i = 123456;
$si = number_format($i,2,".",",");
echo $si;
?>

Four. Mathematical functions

abs() Absolute value
floor() Rounding by rounding
ceil() Rounding by one-digit number
round() Rounding
min( ) Find the minimum value or the minimum value in an array
max() Find the maximum value in an array

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's learning.

Related recommendations:

PHPExcel implements the method of reading excel files and simple examples

PHP obtains form data and HTML embeds PHP scripts Implementation

PHP MVC framework skymvc supports multi-file upload implementation method

The above is the detailed content of Example analysis of PHP mathematical operations and data processing methods. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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