Checking $ _REQUEST for int

I am trying to perform a basic operation: check if a string is a number.

This does not work:

$qty = $_REQUEST[qty]; if (is_int($qty) == FALSE) { echo "error"; } else { echo "ok"; } 

It does:

 $qty = 1; if (is_int($qty) == FALSE) { echo "error"; } else { echo "ok"; } 

$_REQUEST[qty] sent with an AJAX request ($.post). $_REQUEST[qty] ($.post). $_REQUEST[qty] NOT empty and contains only the number (1).

is_numeric() will not work as it treats 1e4 as a number.

+4
source share
7 answers

is_int only returns true if the variable is an integer type. if you are trying to check if a variable contains a string representing a number, Application:

 is_numeric("1"); 

source: http://php.net/manual/en/function.is-int.php

EDIT:

use ctype_digit () to check each character in a string if it is a number to exclude "1e4"

+6
source

If you want to check for only numbers, you can use ctype_digit .

+4
source

try "is_numeric ()" instead of "is_int" ...

I think you will get a string from your query ... and is_int really checks if the given object has an integer ... But this is not โ†’ it is a string.

is_numeric just checks to see if an object can be converted to an integer. If so, it returns true, otherwise false ...

 $qty = $_REQUEST[qty]; if (is_numeric($qty) == FALSE) { echo "error"; } else { echo "ok"; } 

PS: use $ _POST [] or $ _GET [] insert $ _REQUEST [];)

+2
source

You mentioned that you cannot use is_numeric because it treats 1e4 as a number. Well, 1e4 is a number. In particular, 1 * 10^4 .

You can use is_int(intval($_REQUEST['qty'])) , but since intval always returns int (0 on failure or empty input), you risk false positives. However, in combination with is_numeric or filter_var you should be on a fairly solid basis.

+2
source

Make a non-identical comparison & shy; Docs when using string and integer:

 if ($qty !== (string)(int)$qty) { echo "error"; } else { echo "ok"; } 

This is basically literal: since all of the input variables are strings, you cannot verify that they are integers unless you pass them to integers, and then return to the string. Wrap it in a function if it's hard to understand the inline code, what it does:

 /** * string is integer value? * * @return bool */ function is_int_string($string) { return $string === (string)(int)$string; } $qty = $_REQUEST[qty]; if (is_int_string($qty) == FALSE) { echo "error"; } else { echo "ok"; } 

HTTP is a text protocol, so there is only a string at the beginning.

+2
source

In fact, all the values โ€‹โ€‹of $ _REQUEST (as well as $ _GET, $ _POST, etc.) are always strings .

When $qty is $_REQUEST[qty] , it is a string, not an integer. When $qty is 1 , it is already an integer.

Use the intval function to convert it to an integer. But, as you say, you want to know if it is an integer or not, so use floatval to convert it, and then check if they are equal:

 if (intval($qty) == floatval($qty)) { echo "Ok!"; } else { echo "error"; } 
+2
source

I decided to go with preg_match ().

 if (!preg_match("/^[0-9]+$/", $_REQUEST[qty])) {} 
0
source

Source: https://habr.com/ru/post/1388934/


All Articles