<?php

$not_an_int = "sa';';';'sd12340asd";
$actual_int = "12341234";

var_dump(is_int((int)$not_an_int));
echo "<br />";
var_dump(is_int((int)$actual_int));
echo "<br />";
echo "<br />";
var_dump(is_int($not_an_int));
echo "<br />";
var_dump(is_int($actual_int));

?>

Returns:
true
true

false
false

Without casting, real integer is seen as false.
With casting, a non-integer is seen as true.

What would be a real method to detect both "2139481" and 29481932 as integers?

Recommended Answers

All 7 Replies

That's the thing. I don't need it to return string or integer. I need it to return true if element is entirely integer (or integer within quotes "12943"), or false when it's not so. It's not about getting integers out of string, but checking if said variable is an integer.

Line 4 is a string to me. If you cast it, it is what you cast it to. Which is proper.

So code as presented works as expected.

Indeed. That's why I'm seeking for alternative solution. How to detect "10994234" and 3242144122 as integers. Casting doesn't work as mentioned above (false positives).

Also:

filter_var($not_an_int, FILTER_VALIDATE_INT);

Returns FALSE when it's not an integer: http://ideone.com/O3wyIS

commented: I favour this one +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.