validating a number

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
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

validating a number

Post by hame22 »

sorry people but how do i validate a string so i can check that it is an integer?

thanks

I have this at the moment:

Code: Select all

if ($value != strval())
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Code: Select all

if(is_int($value))
which can be sometimes misused because sometimes people use it on numbers that start with 0.. which isn't an integer.

In that case, you should use is_numeric()
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

<?php

if (strval(intval($num)) === $num) {

    //$num is a string representation of an int

}

?>
is_int() would fail because the OP wants to test a string, thus not an int. :)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

but PHP isn't type strict? "6" is the same as 6? that's what I've come to understand.

but anyways, help me learn here :P
how do you know the OP is looking for a string?

couldn't $value be an int or a string?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

Code: Select all

if (preg_match("/^[0-9]$/", $var))
Last edited by raghavan20 on Mon Jan 09, 2006 2:06 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

don't forget the ctype functions, and is_numeric()
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Can only agree with feyd. Relying on a regex to check for a number as string is OTT, unless you require a specific format. Otherwise use the far more scaleable is_numeric(). This will validate any variable type as numeric (though not necessarily an integer!)

ctype_digit() generally accepts only known strings - so be careful you don't use it where the parameter could be a valid integer, or use ctype_digit( (string) $somevar ); to make certain.

If that confused you - use is_numeric(), then cast to integer if that's the number type you expect.
Post Reply