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

Tutorial PHP Base

Tutorial PHP Avanzato

PHP & MySQL

Manuale di riferimento PHP

Operatori matematici PHP

In questo tutorial, imparerai come eseguire operazioni matematiche in PHP.

Eseguire operazioni matematiche

PHP ha molte funzioni interne che possono aiutarti a eseguire tutte le operazioni, dal semplice addizione e sottrazione all'elaborazione di calcoli avanzati. Hai giàOperatori PHPNella sezione abbiamo appreso come eseguire operazioni matematiche di base. Vediamo un altro esempio:

<?php
echo 7 + 3; // Output: 10
echo '<br>';
echo 7 - 2; // Output: 5
echo '<br>';
echo 7 * 2; // Output: 14
echo '<br>';
echo 7 / 2; // Output: 3.5
echo '<br>';
echo 7 % 2; // Output: 1
?>
Prova a vedere‹/›

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 expression inside the parentheses is always evaluated first, as shown in the following examples:

<?php
echo 5 + 4 * 10; // Output: 45
echo '<br>';
echo (5 + 4) * 10; // Output: 90
echo '<br>';
echo 5 + 4 * 10 / 2; // Output: 25
echo '<br>';
echo 8 * 10 / 4 - 2; // Output: 18
echo '<br>';
echo 8 * 10 / (4 - 2); // Output: 40
echo '<br>';
echo 8 + 10 / 4 - 2; // Output: 8.5
echo '<br>';
echo (8 + 10) / (4 - 2); // Output: 9
?>
Prova a vedere‹/›

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

Find the absolute value of a number

can be found using the abs() functionIntegerorFloating point numberThe absolute value, as shown in the following examples:

<?php
echo abs(5) . "<br>"; // Output: 5 (integer)
echo abs(-5) . "<br>"; // Output: 5 (integer)
echo abs(4.2) . "<br>"; // Output: 4.2 (double/float)
echo abs(-4.2) . "<br>"; // Output: 4.2 (double/float)
?>
Prova a vedere‹/›

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

Round up or down decimal values

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:

<?php
// Round up the fraction
echo ceil(4.2) . "<br>"; // Output: 5
echo ceil(9.99) . "<br>"; // Output: 10
echo ceil(-5.18) . "<br>"; // Output: -5
 
// Arrotonda al numero inferiore
echo floor(4.2) . "<br>"; // Output: 4
echo floor(9.99) . "<br>"; // Output: 9
echo floor(-5.18) . "<br>"; // Output: -6
?>
Prova a vedere‹/›

Trova la radice quadrata di un numero

Puoi utilizzare la funzione sqrt() per trovare la radice quadrata di un numero positivo. Se il numero è negativo, restituirà NaN. Ecco un esempio:

<?php
echo sqrt(9) . "<br>"; // Output: 3
echo sqrt(25) . "<br>"; // Output: 5
echo sqrt(10) . "<br>"; // Output: 3.1622776601684
echo sqrt(-16) . "<br>"; // Output: NAN
?>
Prova a vedere‹/›

Genera un numero casuale

La funzione rand() può essere utilizzata per generare numeri casuali. È possibile specificare l'intervallo passando i parametri min e max, come negli esempi seguenti:

<?php
// Genera alcuni numeri casuali
echo rand() . "<br>";
echo rand() . "<br>";
 
// Genera alcuni numeri casuali tra 1 e 10 (compresi 1 e 10)
echo rand(1, 10) . "<br>";
echo rand(1, 10) . "<br>";
?>
Prova a vedere‹/›

Se si chiama la funzione rand() senza parametri opzionali min e max, restituirà un numero pseudo-casuale compreso tra 0 e getrandmax(). La funzione getrandmax() mostra il valore casuale massimo, che su piattaforma Windows è solo 32767. Pertanto, se si ha bisogno di un intervallo superiore a 32767, è sufficiente specificare i parametri min e max.

La conversione tra numero decimale e numero binario

La funzione decbin() viene utilizzata per convertire un numero decimale in un numero binario. Mentre la funzione corrispondente bindec() converte un numero da binario a decimale.

<?php
//Convertire decimale in binario
echo decbin(2) . "<br>"; // Output: 10  
echo decbin(12) . "<br>"; // Output: 1100  
echo decbin(100) . "<br>"; // Output: 1100100
 
// Convertire binario in decimale
echo bindec(10) . "<br>"; // Output: 2 
echo bindec(1100) . "<br>"; // Output: 12  
echo bindec(1100100); // Output: 100
?>
Prova a vedere‹/›

Conversione tra numero decimale e numero esadecimale

La funzione dechex() viene utilizzata per convertire un numero decimale in rappresentazione esadecimale. La funzione hexdec() viene utilizzata per convertire una stringa esadecimale in numero decimale.

<?php
// Convertire decimale in esadecimale
echo dechex(255) . '<br>'; // Output: ff
echo dechex(196) . '<br>'; // Output: c4
echo dechex(0) . '<br>'; // Output: 0
 
// Convertire esadecimale in decimale
echo hexdec('ff') . '<br>'; // Output: 255
echo hexdec('c4') . '<br>'; // Output: 196
echo hexdec(0); // Output: 0
?>
Prova a vedere‹/›

Conversione tra numero decimale e ottale

La funzione decoct() viene utilizzata per convertire un numero decimale in rappresentazione ottale. La funzione octdec() viene utilizzata per convertire un numero ottale in decimale.

<?php
// Convertire decimale in ottale 
echo decoct(12) . '<br>'; // Output: 14
echo decoct(256) . '<br>'; // Output: 400
echo decoct(77) . '<br>'; // Output: 115
 
// Convertire ottale in decimale
echo octdec('14') . '<br>'; // Output: 12
echo octdec('400') . '<br>'; // Output: 256
echo octdec('115'); // Output: 77
?>
Prova a vedere‹/›

Convertire un numero da un sistema numerico a un altro

La funzione base_convert() può essere utilizzata per convertire un numero da un sistema numerico a un altro. Ad esempio, è possibile convertire un numero decimale (base 10))convertito in binario (base 2)),esadecimale (base 16))convertito in ottale (base 8)),ottale in esadecimale, esadecimale in decimale, ecc.

Questa funzione accetta tre parametri: il numero da convertire, la base corrente e la base da convertire. La sintassi di base è la seguente:

base_convert(number,frombase,tobase);

frombase e tobase devono essere tra 2 e 36 (inclusi 2 e 36). I numeri con base maggiore di 10 vengono rappresentati con le lettere a-z, dove a rappresenta 10, b rappresenta 11, z rappresenta 35. Ecco un esempio semplice che spiega come funziona questa funzione:

<?php
//Convertire decimale in binario
echo base_convert('12', 10, 2) . "<br>"; // Output: 1100
// Convertire binario in decimale
echo base_convert('1100', 2, 10) . "<br>"; // Output: 12
 
// Convertire decimale in esadecimale
echo base_convert('10889592', 10, 16) . "<br>"; // Output: a62978
// Convertire esadecimale in decimale
echo base_convert('a62978', 16, 10) . "<br>"; // Output: 10889592
 
// Convertire decimale in ottale
echo base_convert('82', 10, 8) . "<br>"; // Output: 122
// Convertire ottale in decimale
echo base_convert('122', 8, 10) . "<br>"; // Output: 82
 
// Convertire esadecimale in ottale
echo base_convert('c2c6a8', 16, 8) . "<br>"; // Output: 60543250
// Convertire ottale in esadecimale
echo base_convert('60543250', 8, 16) . "<br>"; // Output: c2c6a8
 
// Convertire ottale in binario
echo base_convert('42', 8, 2) . "<br>"; // Output: 100010
// Convertire binario in ottale
echo base_convert('100010', 2, 8) . "<br>"; // Output: 42
 
// Convertire esadecimale in binario
echo base_convert('abc', 16, 2) . "<br>"; // Output: 101010111100
// Convertire binario in esadecimale
echo base_convert('101010111100', 2, 16); // Output: abc
?>
Prova a vedere‹/›

Attenzione:La funzione base_convert() restituirà sempre un valore di tipo stringa. Se il valore restituito è basato su 10, la stringa risultante può essere utilizzata come stringa numerica nel calcolo e PHP la convertirà in numero durante l'esecuzione del calcolo.