0
<?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?

4
Contributors
7
Replies
36
Views
9 Months
Discussion Span
Last Post by diafol
0

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.

0

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.

0

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).

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.