[PHP] is_int fails on checking integer?

Hi all,

Can someone explain me situation from attached screen capture?

2188

PHP fails to find that an integer is an integer.png

Patient ID is a number ([i]three /i seems to be a number right?) sent as string (part of URL), then cast to integer, but even so PHP fails to detect that it actually is a number. And instead of doing some DB-stuff it is throwing an exception I’ve programmed?

I’m totally out of ideas, how it is possible that this particular if is returning false in this particular situation, as on example.

Any ideas will be highly apperciated!

Thanks and cheers,

Trejder

Sorry, probably not helping here but patient or patient id? You’re certain you are interacting with the correct instance member?

EDIT: I can’t create a condition where you cast to integer using (int) where is_int then fails. I tried casting all sorts of stuff and if (int) finds it isn’t an integer it just returns 0. Which is an integer!

I have strict error reporting turned on in my PHP, which means that if I would mistakenly changed variable name, as I think, you suggest, my application would fail to run.

But even so, I’ve double checked that yes, I’m using exactly the same variable in is_int checking and then displaying it in an exception. I forgot to add that those two double quotes, you see on screenshot are added by me in CException message, so they don’t interfere with checked variable type.

Seems that PHP fails to check that variable is a number, raises exception and in that exception message you can see that variable actually is a number. Madness!

Since my first post here, I’ve changed is_int to is_numeric, but it doesn’t change anything. PHP also fails on it. But that isn’t something strange, since - if I’m not mistaken - is_numeric($var) = is_int((int)$var).

AFAIK, is_int (and is_numeric) should return you BOOLEAN, not an integer. And this should be the result of checking, whether your variable is or isn’t an integer.

I meant casting with (int) ALWAYS results in an integer, try it, it will just cast to 0 if it’s not cast-able in any other way! That’s why your problem is really weird, I know that is_int returns an boolean. But doing is_int((int)$notanint) is the same as doing if (true) {}.

Try this:





<?php


$a_int =  (int) 'test';


var_dump($a_int);


class madeup {


}


$test = new madeup;


$a_int = (int) $test;


var_dump($a_int);




PHP will only throw a notice on the casting of the class but it will still assign 1 to $a_int.

Apparently NOT, since this piece of code:


if(is_int((int)$model->patient))

as good as:


if(is_numeric($model->patient))

are returning false on $model->patient having integer 100%

Hey, don’t blame me! I also wouldn’t believe in this, if someone would tell me something like that. But the facts are as they are. You have a screen-shot that shows exception that should never happen a the fact that I had to comment-out this check as my application failed to work with it enabled.

is_int vs. is_numeric

is_int: Find whether the type of a given variable is integer

is_numeric: Finds whether the given variable value is numeric (a number or numeric string)


Trejder - from that above your if should return true in any case as the value of the variable is casted to int… so check again your code… put there one die() to see if the code gets there… could be that the exception is fired somewhere else

@mdomba: This is exactly the same problem I’ve asked on the forum many months ago and you gave me right answer! Look again to the code. I again forgot to add that damn else after closure of code for true condition. So exception is thrown everytime, no matter what is_int / is_numeric returns! :confused:

That’s another prove that coding during late night should be strictly prohibited by law! :]

As you did only post an image, and no code can be seen for the if/else statement… that is another prove that if someone whant proper help, needs to make/post proper question with releant info/code… :D

Yes! You’re right. I promise, I won’t code during late night any more! :] It’s bad… at least for me! :]