Forum Moderators: coopster

Message Too Old, No Replies

Kinda wierd

is_int()

         

Readie

1:45 pm on May 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is just something I found kind of wierd - I have the following select menu:

<select name="greater_region">
<option value=""></option>
<option value="1">Northern France</option>
<option value="2">Western France</option>
<option value="3">Eastern France</option>
<option value="4">Central France</option>
<option value="5">South Western France</option>
<option value="6">Southern France</option>
</select>

And, to save myself having to call loads of functions to sanitise this input (the value will ALWAYS be an int here, it's an auto-increment field in the database) I decided I would just peform the following test:
if(isset($_POST) && !empty($_POST)) {
if(isset($_POST['greater_region']) && !empty($_POST['greater_region']) && is_int($_POST['greater_region'])) {
echo 'a';
} else {
echo 'b';
}
}

Which should echo "a", right? The value is an integer, it came from an INT(10) field in a MySQL database. There's nothing else between the quotes and the number.

Except it echoes "b" - and does until I remove is_int() from the if statement.

So, could anyone explain please?

eelixduppy

1:57 pm on May 3, 2010 (gmt 0)



Form input is always a string, which would make is_int() fail. You should be using is_numeric().

[us2.php.net...]

Readie

2:13 pm on May 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ahh, that explains that then.

Thanks :)

Matthew1980

5:00 pm on May 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Readie,

I can't help but wonder if:-

if(isset($_POST['greater_region']) && !empty($_POST['greater_region']) && (int)$_POST['greater_region']))

would do the same this as is_numeric() Just a thought there, because I am pretty sure as (int)$var forces (for lack of a better word) the contents to be a number/Int I use a method similar to this with my scripts.

Glad your sorted anyway :)

Cheers,
MRb

Readie

9:27 pm on May 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Matt,

It was more just me being lazy :)

Turns out to be kinda unessesary though: my very next check is:

$sql = 'SELECT greater_region_name FROM greater_regions WHERE greater_region_id = "' . mysql_real_escape_string(strip_tags($_POST['greater_region'])) . '" LIMIT 1';
$result = mysql_query($sql);
if($gr_name = mysql_fetch_assoc($result)) {
// Continue script
} else {
// Error message
}

greater_region_id is always an int, if it passed that check it's going to be an int.

jatar_k

1:03 pm on May 4, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> (int)$_POST['greater_region']))

I'm not sure what typecasting it as int would do, and I doubt it would do what you think it does.

Matthew1980

1:22 pm on May 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Jatar_K,

Yeah, I thought about it after posting and came to the same conclusion.

I use:-

$page = (int)$_GET['pageNum'];

thereby forcing a whole page number, or at least thats the way I thought as it functioned :)

So rather than evaluation, I assign it, if I have misunderstood its use please correct me...

Cheers,
MRb

jatar_k

2:23 pm on May 4, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



that usage would be correct but doing a typecast with no actual test in a conditional statement was what got me :)