Are you PHP senior?

Check is string numeric

0%

Check is string integer

82%

Check is this string number

18%

11 votes · Closed

Let's test your knowledge. What this check checking? Please explain.

if(is_numeric($something) && (int)$something == $something){
    /* DO SOMETHING */
}
Start a personal dev blog on your domain for free and grow your readership.

3.4K+ developers have started their personal blogs on Hashnode in the last one month.

Write in Markdown · Publish articles on custom domain · Gain readership on day zero · Automatic GitHub backup and more

Comments (9)

j's photo

var_dump((int) "test" == "test"); because 0 has to be equal to "test" ;D ... obviously :)

Show +6 replies
Ivijan (Stefan) Stipic's photo

j Well, I give that like example. Is better to use strict rules inside function arguments but imagine that we expect in that function integer, boolean, float and string. Integer is for ID, float is for price, boolean is for email send and string is for some message. We can use same funcion and simple if/else to separate data and use same function for various things. Is stupid example but I figureout it before some times where I was forced to use same string for various data like I decribe above. In that my problem I was not able to add additional argument or aditional string, just must expect something from database and do with that how I know and separate it properly.

I first time use this in my 14 years of practice.

j's photo

Ivijan (Stefan) Stipic I think it's a good example, esp if we don't use "declare(strict_types=true)" in all files! :) assertion is always better than assumption in my opinion ! :)