Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6641349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:48:01+00:00 2026-05-25T23:48:01+00:00

This isn’t a big issue for me (as far as I’m aware), it’s more

  • 0

This isn’t a big issue for me (as far as I’m aware), it’s more of something that’s interested me. But what is the main difference, if any, of using is_numeric over preg_match (or vice versa) to validate user input values.

Example One:

<?php
    $id = $_GET['id'];
    if (!preg_match('/^[0-9]*$/', $id)) {
        // Error
    } else {
        // Continue
    }
?>

Example Two:

<?php
    $id = $_GET['id'];
    if (!is_numeric($id)) {
        // Error
    } else {
        // Continue
    }
?>

I assume both do exactly the same but is there any specific differences which could cause problems later somehow? Is there a “best way” or something I’m not seeing which makes them different.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:48:02+00:00Added an answer on May 25, 2026 at 11:48 pm

    is_numeric() tests whether a value is a number. It doesn’t necessarily have to be an integer though – it could a decimal number or a number in scientific notation.

    The preg_match() example you’ve given only checks that a value contains the digits zero to nine; any number of them, and in any sequence.

    Note that the regular expression you’ve given also isn’t a perfect integer checker, the way you’ve written it. It doesn’t allow for negatives; it does allow for a zero-length string (ie with no digits at all, which presumably shouldn’t be valid?), and it allows the number to have any number of leading zeros, which again may not be the intended.

    [EDIT]

    As per your comment, a better regular expression might look like this:

    /^[1-9][0-9]*$/
    

    This forces the first digit to only be between 1 and 9, so you can’t have leading zeros. It also forces it to be at least one digit long, so solves the zero-length string issue.

    You’re not worried about negatives, so that’s not an issue.

    You might want to restrict the number of digits, because as things stand, it will allow strings that are too big to be stored as integers. To restrict this, you would change the star into a length restriction like so:

    /^[1-9][0-9]{0,15}$/
    

    This would allow the string to be between 1 and 16 digits long (ie the first digit plus 0-15 further digits). Feel free to adjust the numbers in the curly braces to suit your own needs. If you want a fixed length string, then you only need to specify one number in the braces.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this isn't really an issue, but more of a concern that I would appreciate
This isn't that big of an issue but its been bugging me I wanted
This isn't really a programming question but more about programming and testing tools. Is
This isn't a question that has a black/white yes/no answer, this is more a
This isn't really clearly documented, but a shallow search reveals that RIM's RAPC compiler
This isn't the complete code, but it's the parts that are causing problems. I've
This isn't a big deal of a question but it bothers me, so I
This isn't that complicated of a question, but I can't wrap my head around
This isn't easy to explain, but I'll try my best. The issue has started
This isn't a question but I thought I'd start a thread where links to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.