Regular expression checking for a number. Checking numeric parameters in PHP Php check if a number is an integer


Currently, active work is underway on the site to launch a new section. And when writing some scripts for it, it became necessary to check variables for numbers. And not just a number (after all, 1.5 is also a number), but a whole number. And this is how I did this check.

Checking if a variable is an integer in PHP

To check in PHP whether a number is an integer, we use a regular expression:

Accordingly, if the variable is an integer, we perform the action we need and vice versa.

Checking if a variable is an integer in JavaScript

In JavaScript, things are a little different, and here we need to write our own little function:

Function number_scan(num) ( return (num ^ 0) === num; )

and use it for testing:

function number_scan(num) ( return (num ^ 0) === num; ) var number = "1.3"; if(number_scan(number)) ( // Action if the number is an integer) else ( // Action if the number is fractional or the variable is not a number at all)

Here, too, if a variable is equal to an integer, the action you want is performed and vice versa.

For example, take the following address: http://example.com/price.php?product=859844&page=99.

The script displays a list of prices in stores for the product product; the optional page parameter specifies the page number. If page is not specified and the url looks like http://example.com/price.php?product=859844 , then we display the first page.

Before PHP 5.2.0, the problem could be solved in this simple way.

// Function for getting a parameter that is a natural number // $arr = array of parameters ($GET or $POST), $name = name of the parameter, // The function returns the value of the parameter, or $default if the parameter is missing or incorrect function get_param_nat($ arr, $name, $default=null) ( if (!isset($arr[$name])) return $default; // Check in a very simple way, convert the parameter to a number, then back to a string // If everything is fine, then the resulting string must match the original value of the parameter $val = $arr[$name]; $intval = intval($val); // For the load, we check that the number we got is greater than zero if (strval($intval) != = $val || $intval< 1) return $default; return $intval; } // Проверяем параметр product if (($product = get_param_nat($_GET, "product")) === null) die("Product not found"); // Получаем номер страницы $page = get_param_nat($_GET, "page", 1);

Starting in version 5.2.0, a group of Filter functions appeared in PHP: filter_var, filter_input, filter_var_array and several others. Functions can check variables for integers, floating point numbers, e‑mail, ip‑addresses, url, etc., and also clear strings according to specified parameters.

Let's rewrite the code using the filter_input function.

Function get_param_nat($type, $name, $default=null) ( $val = filter_input($type, FILTER_VALIDATE_INT, array("min_range" => 1, "max_range" => PHP_INT_MAX)); // filter_input returns false if filtering failed, or null if the variable is undefined if ($val === null || $val === false) return $default; return $val; ) // Check the product parameter if (($product = get_param_nat(INPUT_GET , "product")) === null) die("Product not found"); // Get the page number $page = get_param_nat(INPUT_GET, "page", 1);

A little testing showed that the speed of operation of the two variants of the get_param_nat function is almost the same, and in the absence of a parameter, for example, page is not specified, the first variant will work even somewhat faster.

So it's up to you to decide what to use. I personally use the first function; it is somehow closer and clearer to me.

Well, a small lyrical digression at the end. To avoid duplicating the page for search engines, for example, without page and with page=1, I recommend specifying the canonical url in the head section of the page. It will look something like this:

...







2024 gtavrl.ru.