R
R
Rishat Sultanov2018-03-26 20:23:38
PHP
Rishat Sultanov, 2018-03-26 20:23:38

What about is_numeric() if it skips spaces?

Hello everyone :)
In general, there is such a story. I decided to check GET parameters for numbers, and then I got carried away.
I started digging and realized that if you pass a string with spaces and a number as a GET parameter in the url, then is_numeric () eats it and spits out true. And I wouldn't want that to happen. Of course, now I've decided everything like this:

$key_from_get = isset($_GET["key"]) ? $_GET["key"] : "";
$iin_from_get = isset($_GET["iin"]) ? $_GET["iin"] : "";

$key = str_replace(' ', '', $key_from_get);
$iin = str_replace(' ', '', $iin_from_get);

if(is_numeric($key) && is_numeric($iin)){
    TrekInfo($key,$iin); // Отправляем данные в API
}
else{
    echo 'Упс! Что-то не так с параметрами запроса!';
}

Well, I think that it can be shorter and better ... Are there options or is this option normal? Thank you very much :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Hog, 2018-03-26
@rishatss

If you expect only numbers and they cannot be null, you can cast the parameters to integer.

$key = isset($_GET['key']) ? (int)$_GET['key'] : null;
$iin = isset($_GET['iin']) ? (int)$_GET['iin'] : null;

if(!empty($key) && !empty($iin)) {
    TrekInfo($key, $iin); // Отправляем данные в API
} else {
    echo 'Упс! Что-то не так с параметрами запроса!';
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question