0

I know 0 is an integer, and

if((int)$some_data)

will return true(1) if the input is an integer and false(0) if not. I was trying to find whether 0 is a integer or not using a if condition, and the a simple example is here

$input= 0;

if((int)$input)
    echo "Yes";
else
    echo "No";

when I run this code, It returns "No", which means if condition returns 0 is not an integer. And I know that happens because

(int)0 returns 0

which means if(0) always returns false condition

so how can I find whether 0 is a integer or not. I don't know the question is duplicate or not, but I googled and still couldn't found a satisfying answer. If someone explain how to find whether 0 is int or not. Thanks in advance.

Bergin
  • 188
  • 3
  • 12
  • 5
    You're doing a cast, not a test. Use `is_int` to test if it's an int. http://php.net/manual/en/function.is-int.php – Jules R Oct 12 '18 at 12:25
  • can you please check whether "00" is a integer or not using is_int() function. it will return false if you use double or single quotes(which is a string) – Bergin Oct 12 '18 at 12:30
  • Or use `$x === 0` to confirm `$x` is zero *and* an integer if you like – avy Oct 12 '18 at 12:30
  • `00` is never an integer, because _real_ numbers don’t have such leading zeroes. You should really be more specific as to what you actually want to test for here, resp. what results you need. Give proper examples of different inputs, and what output you need for each of them. – misorude Oct 12 '18 at 12:37

5 Answers5

4

You have to use is_int()

if(is_int($input))
    echo "Yes";
else
    echo "No";

Note:- in your code you are doing casting, so it become if(0) which will always return false

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
3

The answer is pretty simple, but to understand why your code does not work you have to know that

(int)$input

is not a check, but is a cast. A cast is how we force a variable to be our desired variable type. So, basically you're saying to PHP "Hey PHP, no matter what is into $input, I want it to be an integer", PHP will try its best to force $input as an integer and, in your case, will return a 0.

Now, what happens if you try to do this

if (0)

simple, it will always be false.

Instead of casting $input use is_int()

Alpe89
  • 299
  • 2
  • 11
  • what will happen if i change the input value as $input = "0" or $input = "1" and using is_int() to find the it is int or not? – Bergin Oct 12 '18 at 12:32
  • 1
    in boolean logic 0 means false, 1 means true. is_int() just takes an input and returns true or false based on the fact that the input is indeed a number. So, is_int(0) will be true, is_int(1) will also be true. – Alpe89 Oct 12 '18 at 12:34
2

is_int will check the type of the variable passed to it, but from your comments it seems like you're actually looking to test whether or not a string consists of digits. In that case, ctype_digit might be more useful:

$string = '0';
var_dump(ctype_digit($string));

bool(true)

https://3v4l.org/0j56h

iainn
  • 16,826
  • 9
  • 33
  • 40
1

Try This

$input= 0;

if(is_int($input))
    echo "Yes";
else
    echo "No";
Sanu0786
  • 571
  • 10
  • 15
1

i'm recommendation to use you with is_numeric

if (is_numeric(0)) { echo "Yes"; } else { echo "No"; } 
// OUTPUT = YES

DEMO

Dave
  • 3,073
  • 7
  • 20
  • 33