Math functions can be considered as programming heart or brain: they are paramount to resolve any and all logical problems that may arise. In PHP, the mathematical operators are worked out very simply, also in a standardized way like in other languages.

php_calculation

Then we go to a simple example that shows how the operations can be performed:

<?php

$var2 = 2;

$var = $var2 +2; //  Sum

$var = $var2 -2; // Subtraction

$var = $var2/2; //  Division

$var = $var2*2; // Multiplication

?>

Note that the mathematical operators do not change, all to facilitate the assimilation and the lives of those who need to solve complex problems.

Besides the basic operators, the language still offers some ready calculations. The main ones will be showed now, all aimed at making programmer’s life easier.

ABS FUNCTION

This function returns the absolute value of a variable, as shown in the example:

<?php
$abs = abs(-4.2); // $abs = 4.2; (double/float)
$abs2 = abs(5);   // $abs2 = 5; (interger)
$abs3 = abs(-5);  // $abs3 = 5; (interger)
?>

CEIL FUNCTION

This function rounds the value reported to the nearest integer, according to a mathematical rule. As shown in the example:

<?php
echo ceil(4.3);    // 5
echo ceil(9.999);  // 10
echo ceil(-3.14);  // -3
?>

 

FLOOR FUNCTION

This function rounds the fractional values to the lowest value, for example:

<?php
echo floor(4.3);   // 4
echo floor(9.999); // 9
echo floor(-3.14); // -4
?>

 

MAX FUNCTION

From the numbers of an array, the function checks which one is bigger, as the example:

<?php
echo max(1, 3, 5, 6, 7);  // 7
echo max(array(2, 4, 5)); // 5

?>

MIN FUNCTION

From the number of an array, the function checks which one is the smallest, as the example:

<?php
echo min(1, 3, 5, 6, 7);  // 1
echo min(array(2, 4, 5)); // 2

?>

 

MT_RAND FUNCTION

This function returns a random value. If the start and end parameters are informed, the generated value will be in the range between them. For example:

<?php
echo mt_rand() . “n”;
echo mt_rand() . “n”;

echo mt_rand(5, 15);
?>

The above example will output something similar to:

1604716014

1478613278

6

 

SQRT FUNCTION

The square root calculation can be done using this function, as shown in the example:

<?php
echo sqrt(9); // 3
echo sqrt(10); // 3.16227766 …
?>

 

 

 

A number of other functions can be found in the official documentation of the language. The ones shown above are the most common, used for solving practically all kinds of calculations. Anyway, it is important to know and get referrals as a particular problem may have a ready solution. So, knowing where to seek information turns out to be highly relevant and productive.

You might also like…

PROCESAMIENTO AJAX EN SCRIPTCASE

Ajax en los formularios de Scriptcase puede usarse para recargar un campo de tipo de select basado e...

IMPORTAR IMAGENES DE LA BASE DE DATOS Y UTILIZAR CLASES DE BOOSTRAP

Como muchas personas ya saben, Bootstrap es una biblioteca de front-end libre y de código abierto p...

TENDENCIAS DE DESARROLLO WEB PARA 2017

Usted verá en este post tendencias para 2017 en Diseño Web, en el medio digital y en el desarrollo...

Comment this post

Get new posts, resources, offers and more each week.

We will use the information you provide to update you about our Newsletter and Special Offers. You can unsubscribe any time you want by clinck in a link in the footer of any email you receive from us, or by contacting us at sales@scriptcase.net. Learn more about our Privacy Police.