22, August 2008

array vals are numeric check - webmaster forum

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: array vals are numeric check
« previous next »
Pages: [1] Print

Author Topic: array vals are numeric check  (Read 307 times)
Cyberpunk Wannabe
*
Posts: 46
352 credits
Members referred : 0


« on: Apr 24, 2008, 12:07:57 AM »

is there a easy way to check if the values are in numeric only.

i did this to do a simple check, but didn't know if there was a better function to do it with.
Quote
foreach ($arrayas $check) {
                           if (!intval($check)) {
                              $error = "Invaild selection";
                           }
                        }
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8020
41077 credits
Members referred : 3



« Reply #1 on: Apr 24, 2008, 07:52:53 AM »

I usually use the (int) function, but the way you are using is ok.

Here is an example :

$id = (int)$_GET['id'];
if ( 
$id == ) die('problem');


So whatever the user sent to the id variable, if the value is not numeric it will be converted to 0

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
Nikolas' Servant Child
*
Posts: 30
184 credits
Members referred : 0


« Reply #2 on: Apr 25, 2008, 11:27:27 AM »

is there a easy way to check if the values are in numeric only.

Why not use is_numeric()?

Code:
foreach ($array as $check) {
    if (!is_numeric($check)) {
        $error = "Invaild selection";
    }
}
Cyberpunk Wannabe
*
Posts: 46
352 credits
Members referred : 0


« Reply #3 on: Apr 25, 2008, 06:42:27 PM »

because i completely forgot about that command lol
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 8020
41077 credits
Members referred : 3



« Reply #4 on: Apr 28, 2008, 11:47:45 AM »

I think (int) is faster than is_numeric, that's why I am using it, and I do it just to validate data that come from $_GET or $_POST

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=7707
Tags : php array numeric Bookmark this thread : Digg Del.icio.us Dzone more....

Topic sponsors:
Get a permanent link here for $1.99!


Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: array vals are numeric check
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Aug 22, 2008, 03:16:13 AM





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!


Forum Statistics
Total Posts: 36.084
Total Topics: 7.439
Total Members: 3.807
Tutorials : 56
Resources : 143
Designs : 220
Latest Member: marthawelch

28 Guests, 2 Users online :

16 users online today:



Readers

Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2008 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.