Broad Network


Mathematics Functions for Everybody in PHP

Foreword: In this tutorial I show you how to use maths functions that everybody needs to know how to use, using the PHP computer language.

By: Chrysanthus Date Published: 29 Jan 2019

Introduction

In this tutorial I show you how to use maths functions that everybody needs to know how to use, using the PHP computer language.

The abs() Function
An integer can be positive or negative, e.g +5 and -5. +5 is the same as 5. A float can be positive or negative, e.g. +2.4 and -2.4. +2.4 is the same as 2.4.

The abs() function takes a number as argument and returns it without the sign. In other words, it returns the positive number (version). Try the following code:

<?php

    echo abs(+5), '<br>';
    echo abs(-5), '<br>';
    echo abs(+2.4), '<br>';
    echo abs(-2.4), '<br>';

?>

The output is:

    5
    5
    2.4
    2.4

The round() Function
When rounded, 2.5 becomes 3, 2.7 becomes 3; but 2.4 becomes 2 and 2.1 becomes 2.

The round() function returns a float number rounded. Try the following code:

<?php

    echo round(2.5), '<br>';
    echo round(2.7), '<br>';
    echo round(2.4), '<br>';
    echo round(2.1), '<br>';

?>

The output is:

    3
    3
    2
    2

The ceil() Function
This function rounds a number up. This means that any decimal part, whether less than .5 or not, is taken up to 1, and added to the whole number part. Try the following code:

<?php

    echo ceil(2.5), '<br>';
    echo ceil(2.7), '<br>';
    echo ceil(2.4), '<br>';
    echo ceil(2.1), '<br>';

?>

The output is:

    3
    3
    3
    3

The floor() Function
This function rounds a number down. This means that any decimal part, whether less than .5 or not, is taken down to 0, and nothing is added to the whole number part. Try the following code:

<?php

    echo floor(2.5), '<br>';
    echo floor(2.7), '<br>';
    echo floor(2.4), '<br>';
    echo floor(2.1), '<br>';

?>

The output is:

    2
    2
    2
    2

This function returns FALSE in case of an error (e.g. passing an array).

The getrandmax() Function
This function returns the maximum possible random integer that PHP (rand()) can give. Try the following code:

<?php

    $inte = getrandmax();
    echo $inte;

?>

I tried it and I had:

    2147483647

The rand() Function
This function returns a pseudo random whole number between 0 and the value returned by getrandmax(). The word, pseudo, means "not genuine". The function has two syntaxes. One is:

    int rand ( void )

<?php

    $inte = rand();
    echo $inte;

?>

I tried it and I had:

    1047134471

If you want the return integer to be between a minimum value and a maximum value, you have to use the syntax:

    int rand ( int $min , int $max )

where $min is greater than or equal to 0 and $max is less than or equal to the value returned by getrandmax(). Try the following code:

<?php

    $inte = rand(6, 17);
    echo $inte;

?>

I tried it the first time and I had 7, the next number after 6, suggesting that the function does not truly return a random number.

Security Considerations

The floor() Function
The floor() function returns a rounded-down number, or false in case of an error. So, do not consider false as 0 for the returned value. Test for the returned value with code similar to the following:

<?php

    $variab = 0.6;
    $ret = floor($variab);

    if ($ret === false)
        echo "Error occurred! Maybe argument is an array.<br>";
    else
        echo "Use $ret .<br>";

?>

The rand() Function
This function returns a pseudo (not genuine) random whole number between 0 and the value returned by getrandmax(). An attacker (hacker) can predict the returned value, though with some difficulties. If you want a returned value, where an attacker would predict with more difficulties, use the function:

    int random_int ( int $min , int $max )

The function generates cryptographic random integers that are suitable for use where unbiased results are critical, such as when shuffling a deck of cards for a poker game.

The function returns a cryptographically secure random integer in the range min to max, inclusive.

That is it for this part of the series.

Chrys


Related Links

Basics of PHP with Security Considerations
White Space in PHP
PHP Data Types with Security Considerations
PHP Variables with Security Considerations
PHP Operators with Security Considerations
PHP Control Structures with Security Considerations
PHP String with Security Considerations
PHP Arrays with Security Considerations
PHP Functions with Security Considerations
PHP Return Statement
Exception Handling in PHP
Variable Scope in PHP
Constant in PHP
PHP Classes and Objects
Reference in PHP
PHP Regular Expressions with Security Considerations
Date and Time in PHP with Security Considerations
Files and Directories with Security Considerations in PHP
Writing a PHP Command Line Tool
PHP Core Number Basics and Testing
Validating Input in PHP
PHP Eval Function and Security Risks
PHP Multi-Dimensional Array with Security Consideration
Mathematics Functions for Everybody in PHP
PHP Cheat Sheet and Prevention Explained
More Related Links

Cousins

Comments