int is not an int!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
slash_gnr3k
Forum Commoner
Posts: 43
Joined: Tue Nov 28, 2006 6:41 pm

int is not an int!

Post by slash_gnr3k »

i have an input form with some fields. obviously i dont want people to add strings to text fields.

this is the attribute in question:

Code: Select all

<p><input type = "text" name = "newpreptime" /></p>
when the form is submitted, this happens:

Code: Select all

$newpreptime=$_POST['newpreptime'];
and this is the test

Code: Select all

					if (is_int($newpreptime))
					{

						echo"this is a int";
					}
	
					else
					{
						echo"this is not a int";
					}
for some reason if i type in an integer or a string, this test always fails and the echo statement fires saying it is not an int. i thought php automatically converted strings to int if valid?
can anyone help?
thanks
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

var_dump($newpreptime) would reveal it actually is not an int, but a string ;)

your looking for is_numeric()
Mohit_Prog
Forum Commoner
Posts: 26
Joined: Mon Apr 23, 2007 6:10 am

Post by Mohit_Prog »

any request coming from a textbox ias always referred as a string.
You have to manually check with some PHP functions or type cast into int.

otherwise you can put validation when you are filling form that only numbers should be entered.
and on the next page typecast it into an int. so simple....
Post Reply