is_int()

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

I have the following two lines of code:

!isset($_POST['rows']) ? $rows = "10" : $rows = trim($_POST['rows']);
!is_int($_POST['rows']) ? $rows = "10" : $rows = trim($_POST['rows']);
'

What this is ment to do is check if $_POST['rows'] is set. If not, then set it to "10" else just leave it.
Next, it checks if $_POST['rows'] is an integer or not. If it isn't, then set it to "10" else, just leave it.

No matter what number or text I send, it always returns a "10"

Here is the "working program"

I tried looking on php.net for the is_int() function use, but, that didn't really help much...

Busy's picture

He has: 6,151 posts

Joined: May 2001

tried printing the values inbetween?

!isset($_POST['rows']) ? $rows = "10" : $rows = trim($_POST['rows']);
echo "rows = $rows";
!is_int($_POST['rows']) ? $rows = "10" : $rows = trim($_POST['rows']);
echo "rows now = $rows";

CptAwesome's picture

He has: 370 posts

Joined: Dec 2004

Ah, I think your problem is trim:

Quote: string trim ( string str [, string charlist] )

This function returns a string with whitespace stripped from the beginning and end of str.

When you use trim, it becomes a string, instead of an interger, and will always fail is_int(); (thats my best guess)

I'd suggest doing just an is_int anyway, if it fails that, then it's obviously going to fail isset right? so, I owuld simply have:
!$is_int($_POST['rows'] ? $_POST['rows'] = 10;
that pretty much covers all your bases, but I would set a min value and a max value if you haven't already.

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

The problem has been solved. Thank you.

It turned out that passing anything through POST makes it a string, so I had to use is_gnumeric() instead. Smiling

CptAwesome's picture

He has: 370 posts

Joined: Dec 2004

Ah, I was testing it with $_GET

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.