Broad Network


PHP Core Number Basics and Testing

Foreword: In PHP, a number can be a whole number, an integer, a real number or a real number in exponential form. In this tutorial I talk about numbers, and how they can be tested.

By: Chrysanthus Date Published: 23 Jan 2019

Introduction

In PHP, a number can be a whole number, an integer, a real number or a real number in exponential form. In this tutorial I talk about numbers, and how they can be tested.

Pre-Knowledge
This tutorial is part of the volume, PHP Course. At the bottom of this page you have links to the different series you should have read before coming here.

Numbers

Whole Number
A whole number is an integer without the – or + sign. It is actually a positive number, (also called an unsigned number). It can be assigned to a variable as in the following statement:

    $wholeNum = 25;

Integer
An integer is a whole number, which can be positive or negative. If it is positive, the + sign can be omitted. It can be called a sign number. It can be assign to a variable as in the following cases:

        $inter1 = -246;
        $inter2 = +36;
        $inter3 = 36;

Real Number
A real number is a number with a decimal point. If it is a negative number, it would have the negative sign. If it is a positive number it might or might not have the + sign. It can be assigned to a variable as in:

    $rl = 2.5;

Note that the decimal part of the number may be zero. In that case it is like an integer.

Real Number in Exponential Form
A real number can be written in exponential form. For example, 268.3 can be written as,

    2.683e2

You begin with a single digit whole number; then you have an optional decimal point. After, you have an optional decimal part; then e; and finally a number, which indicates the number of places the decimal point has to be shifted to get the real number. If the decimal point has to be shifted to the left, then this number has to be preceded by - . If the complete number is negative, then the complete number has to be preceded by - .

Testing Numbers
First of all let us see how to test if a variable is a string. The following string matching expression, test if a variable (subject) is a string:

    preg_match("/\D/", subject);

This tests if there is a non-digit character in the string. Try the following code:

<?php

        $str = "We are the world.";

        if (preg_match("/\D/", $str) === 1)
            {
                echo 'Matched';
            }
        else
            {
                echo 'Not Matched or Error occurred!';
            }

?>

Whole Number
The following expression will return 1 if the subject is a whole number:

    preg_match("/^\d+$/", $subject)

In the regex, \d means digit. ^ means match at the beginning of the string. + means match the preceding character 1 or more times. $ means the end of the string. Try the following code:

<?php

        $subject = 25;

        if (preg_match("/^\d+$/", $subject) === 1)
            echo("It is a whole number.");

?>

Integer
The following expression will return 1 if the subject is an integer:

    preg_match("/^[+-]?\d+$/", $subject)

[+-] means + or -. ? means match the preceding character, 0 or 1 time. Try the following code:

<?php

        $subject = "-246";

        if (preg_match("/^[+-]?\d+$/", $subject) === 1)
            echo("It is an integer.");

?>

Real Number
The following expression will return 1 if the subject is a real number (float):

    preg_match("/^-?(?:\d+\.?|\.\d)\d*$/", $subject)

I leave the explanation of the regex as an exercise for you. Try the following code:

<?php

        $subject = "2.5";

        if (preg_match("/^-?(?:\d+\.?|\.\d)\d*$/", $subject) === 1)
            echo("It is a real number.");

?>

Real Number in Exponential Form
The following expression will return 1 if the subject is a real number in exponential form:

    preg_match("/^[+-]?(?=\.?\d)\d*\.?\d*(?:e[+-]?\d+)?$/i", $subject)

Note that the e in the exponential form can also be E; that is why you have /i at the end of the regex. I leave the explanation of the rest of the regex as an exercise for you. Try the following code:

<?php

        $subject = "2.683e2";

        if (preg_match("/^[+-]?(?=\.?\d)\d*\.?\d*(?:e[+-]?\d+)?$/i", $subject) === 1)
            echo("It is a real number.");

?>

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