English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP Mathematical Operators

In this tutorial, you will learn how to perform mathematical operations in PHP.

Performing Mathematical Operations

PHP has multiple built-in functions that can help you perform all operations from simple addition or subtraction to advanced calculations. You have alreadyPHP OperatorsWe have learned how to perform basic mathematical operations in this chapter. Let's look at another example:

<?php
echo 7 + 3echo // Output: 10
;
echo 7 - 2echo // Output: 5
;
echo 7 * 2echo // Output: 14
;
echo 7 / 2echo // Output: 3You can use the sqrt() function to find the square root of a positive number. If the number is negative, it returns NaN. Here is an example:5
;
echo 7 % 2echo // Output: 1
?>
Test to see‹/›

Each mathematical operation has a certain priority; usually, multiplication and division are performed before addition and subtraction. However, parentheses can change this priority. Regardless of the priority of the operation, the expressions within the parentheses are always evaluated first, as shown in the following examples:

<?php
echo 5 + 4 * 10echo         // Output: 45
;
echo '<br>';5 + 4echo ( * 10echo       // Output: 90
;
echo 5 + 4 * 10 / 2echo     // Output: 25
;
echo 8 * 10 / 4 - 2echo     // Output: 18
;
echo 8 * 10 / )4 - 2);   // Output: 40
;
echo 8 + 10 / 4 - 2echo     // Output: 8You can use the sqrt() function to find the square root of a positive number. If the number is negative, it returns NaN. Here is an example:5
;
echo '<br>';8 + 10echo ( / )4 - 2); // Output: 9
?>
Test to see‹/›

(

In the following section, we will learn some built-in PHP functions that are most commonly used for mathematical operations.

Find the absolute value of a numberYou can use the abs() function to findIntegerorFloating point number

<?php
(integer)5) . "<br>";    // Output: 5 The absolute value, as shown in the following examples:
(integer)-5) . "<br>";   // Output: 5 The absolute value, as shown in the following examples:
(integer)4You can use the sqrt() function to find the square root of a positive number. If the number is negative, it returns NaN. Here is an example:2) . "<br>";  // Output: 4You can use the sqrt() function to find the square root of a positive number. If the number is negative, it returns NaN. Here is an example:2 echo abs(/(double
(integer)-4You can use the sqrt() function to find the square root of a positive number. If the number is negative, it returns NaN. Here is an example:2) . "<br>"; // Output: 4You can use the sqrt() function to find the square root of a positive number. If the number is negative, it returns NaN. Here is an example:2 echo abs(/(double
?>
Test to see‹/›

float)

As you can see, if the given number is negative, the returned value is positive. However, if the number is positive, this function only returns the number.

Round up or down the decimal value

<?php
//The ceil() function can be used to round a decimal to the next highest integer, while the floor() function can be used to round a decimal to the next lowest integer, as shown in the following examples:
Fractional part up rounding4You can use the sqrt() function to find the square root of a positive number. If the number is negative, it returns NaN. Here is an example:2) . "<br>";    // Output: 5
Fractional part up rounding9You can use the sqrt() function to find the square root of a positive number. If the number is negative, it returns NaN. Here is an example:99) . "<br>";   // Output: 10
Fractional part up rounding-5You can use the sqrt() function to find the square root of a positive number. If the number is negative, it returns NaN. Here is an example:18) . "<br>";  // Output: -5
 
//echo ceil(
Fractional part down rounding4You can use the sqrt() function to find the square root of a positive number. If the number is negative, it returns NaN. Here is an example:2) . "<br>";    // Output: 4
Fractional part down rounding9You can use the sqrt() function to find the square root of a positive number. If the number is negative, it returns NaN. Here is an example:99) . "<br>";   // Output: 9
Fractional part down rounding-5You can use the sqrt() function to find the square root of a positive number. If the number is negative, it returns NaN. Here is an example:18) . "<br>";  // Output: -6
?>
Test to see‹/›

echo floor(

Find the square root of a number

<?php
.9) . "<br>";   // Output: 3
.25) . "<br>";  // Output: 5
.10) . "<br>";  // Output: 3You can use the sqrt() function to find the square root of a positive number. If the number is negative, it returns NaN. Here is an example:1622776601684
.-16) . "<br>"; // echo sqrt(
?>
Test to see‹/›

Output: NAN

Generate random numbers

<?php
//The rand() function can be used to generate random numbers. You can specify a range by passing min, max parameters, as shown in the following examples:
Generate some random numbers
Generate some random numbers
 
//echo rand() . "<br>";1to10Generate some numbers between1and10)
between (including1, 10) . "<br>";
between (including1, 10) . "<br>";
?>
Test to see‹/›

echo rand(32767If the rand() function is called without optional min, max parameters, it will return a pseudo-random number between 0 and getrandmax(). The function getrandmax() displays the maximum random value, which is only32767Therefore, if you need a number greater than

Range, you can simply specify the min and max parameters.

Conversion between decimal and binary numbers

<?php
//Convert decimal to binary
The decbin() function is used to convert a decimal number to a binary number. The corresponding bindec() function converts a number from binary to decimal.2) . "<br>";    // Output: 10  
The decbin() function is used to convert a decimal number to a binary number. The corresponding bindec() function converts a number from binary to decimal.12) . "<br>";   // Output: 1100  
The decbin() function is used to convert a decimal number to a binary number. The corresponding bindec() function converts a number from binary to decimal.100) . "<br>";  // Output: 1100100
 
//Convert binary to decimal
echo bindec(10) . "<br>";       // Output: 2 
echo bindec(1100) . "<br>";     // Output: 12  
echo bindec(1100100);  // Output: 100
?>
Test to see‹/›

Conversion between decimal and hexadecimal numbers

The dechex() function is used to convert a decimal number to its hexadecimal representation. The hexdec() function is used to convert a hexadecimal string to a decimal number.

<?php
//Convert decimal to hexadecimal
echo dechex(255) . "<br>";  // Output: ff
echo dechex(196) . "<br>";  // Output: c4
echo dechex(0) . "<br>";    // Output: 0
 
//Convert hexadecimal to decimal
echo hexdec('ff') . "<br>";  // Output: 255
echo hexdec('c';4') . "<br>";  // Output: 196
echo hexdec(0);     // Output: 0
?>
Test to see‹/›

Decimal and octal numbers are converted to each other

decoct() function is used to convert a decimal number to its octal representation. octdec() function is used to convert an octal number to a decimal number. The conversion between decimal and octal numbers

<?php
//Convert decimal to octal 
echo decoct(12) . "<br>";   // Output: 14
echo decoct(256) . "<br>";  // Output: 400
echo decoct(77) . "<br>";   // Output: 115
 
//Convert octal to decimal
echo octdec('14') . "<br>";   // Output: 12
echo octdec('400') . "<br>";  // Output: 256
echo octdec('115);  // Output: 77
?>
Test to see‹/›

Convert a number from one base system to another

base_convert() function can be used to convert a number from one base system to another. For example, you can convert decimal (Base10)Convert to binary(Base2), hexadecimal(Base16)Convert to octal(Base8))。 Octal to hexadecimal, hexadecimal to decimal, and so on.

This function accepts three parameters: the number to be converted, the current base, and the base to be converted to. The basic syntax is as follows:

base_convert(number,frombase,tobase);

frombase and tobase must be in2to36between (including2and36)。 Base greater than10The number will be represented by the letter a-z represents, where a represents10, b represents11, z represents35. This is a simple example that shows how this function works:

<?php
//Convert decimal to binary
echo base_convert('12', 10, 2) . "<br>";  // Output: 1100
//Convert binary to decimal
echo base_convert('1100', 2, 10) . "<br>";  // Output: 12
 
//Convert decimal to hexadecimal
echo base_convert('10889592', 10, 16) . "<br>";  // Output: a62978
//Convert hexadecimal to decimal
echo base_convert('a62978', 16, 10) . "<br>";  // Output: 10889592
 
//Convert decimal to octal
echo base_convert('82', 10, 8) . "<br>";  // Output: 122
//Convert octal to decimal
echo base_convert('122', 8, 10) . "<br>";  // Output: 82
 
//Convert hexadecimal to octal
echo base_convert('c2c6a8', 16, 8) . "<br>";  // Output: 60543250
//Convert octal to hexadecimal
echo base_convert('60543250', 8, 16) . "<br>";  // Output: c2c6a8
 
//Convert octal to binary
echo base_convert('42', 8, 2) . "<br>";  // Output: 100010
//Convert binary to octal
echo base_convert('100010', 2, 8) . "<br>";  // Output: 42
 
//Convert hexadecimal to binary
echo base_convert('abc', 16, 2) . "<br>";  // Output: 101010111100
//Convert binary to hexadecimal
echo base_convert('101010111100', 2, 16);  // Output: abc
?>
Test to see‹/›

Note:The base_convert() function will always return a string value. If the returned value starts with10If the base is set to 0, the resulting string can be used as a numeric string in calculations, and PHP will convert it to a number during execution.