Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is_int returns false for negative values like "-10"?

Tags:

php

integer

Does php's is_int function return false for negative numbers because integers in php are unsigned, or is there other behaviour that I'm missing? I'm trying to check if something is a whole number, and I don't want to rely on that behaviour of is_int (for the first part of the test) if it's doing something different.

Clarification: I know that is_int returns false for negative numbers, but I'm asking why because of this behaviour: var_dump(intval("-10")) prints int(-10), and var_dump(intval("10")) prints int(1), so both negative and positive values are considered integers, yet is_int("-10") returns false.

EDIT: Ok, sorry everyone, I got quite a bit confused about the behaviour of is_int and integers in general. I was thinking of it acting on a string with contents like "-10" when what I need is is_numeric on a string, or is_int on an integer itself. Thanks for the help.

like image 871
Ricardo Altamirano Avatar asked Dec 27 '22 15:12

Ricardo Altamirano


2 Answers

It doesn't:

var_dump( is_int(-10) ); // bool(true)

Probably, it isn't a number to begin with:

var_dump( is_int('-10') ); // bool(false)

If so, try out is_numeric(), which is designed for strings.

like image 111
Álvaro González Avatar answered Jan 11 '23 11:01

Álvaro González


Why not test it for yourself?

<?php

var_dump(is_int(-1));
var_dump(is_int('-1'));

produces:

bool(true)
bool(false)
like image 37
Marc B Avatar answered Jan 11 '23 11:01

Marc B