How to Check a Value Is Numeric in PHP

Definition and Usage of is_numeric in PHP

The is_numeric() function describes whether a variable is a number or a string of numbers.

If the variable is a number or a numeric string, this function returns true; otherwise, it returns false/nothing.

Additionally, based on the PHP documentation, is_numeric finds whether a variable is a number or a numeric string.

Syntax:

is_numeric( variable );

Parameter Values

ParameterDescription
variableRequired. Specifies the variable to check

Technical Details

Return Value:TRUE if variable is a number or a numeric string, FALSE otherwise
Return Type:Boolean
PHP Version:4.0+

Is_numeric PHP examples

The following is an example usage of is_numeric:

<?php
    // if it is a number
    $str = "050122";
    if ( is_numeric($str) ) {
        echo "'{$str}' is a numeric.";
    } else {
        echo "'{$str}' is not a numeric.";
    }
    
    echo ("\n");
    
    // if it is a string
    $str = "itsourcecode";
    if ( is_numeric($str) ) {
        echo "'{$str}' is a numeric.";
    } else {
        echo "'{$str}' is not a numeric.";
    }
?>

Output:

'050122' is a numeric.
'itsourcecode' is not a numeric.

Explanation:

The example above illustrates that if the value of a $str value is a number, it will print that it is numeric.

If it is a string, then it will print that it is not numeric.

Is_numeric PHP examples with whitespace

The following is an example usage of is_numeric, but this time, the value of the string has whitespace:

<?php
    // if it is a number
    $str = "05 01 22";
    if ( is_numeric($str) ) {
        echo "'{$str}' is a numeric.";
    } else {
        echo "'{$str}' is not a numeric.";
    }
    
    echo ("\n");
    
    // if it is a string
    $str = "it source code";
    if ( is_numeric($str) ) {
        echo "'{$str}' is a numeric.";
    } else {
        echo "'{$str}' is not a numeric.";
    }
?>

Output:

'05 01 22' is not a numeric.
'it source code' is not a numeric.

Explanation:

The example above illustrates that if the value of a $str value is a number with whitespace, it will print that it is not numeric.

Therefore, if the variable value has whitespace on it, it will return a value of False.

Frequently Ask Question

What is_numeric value in PHP?

PHP has a lot of built-in functions that can be used to check the type of data in a variable.

One of them is called is numeric(). It is used to see if a variable or a string of numbers is a number or a number.

Along with the digits, the string of numbers can have the “+/-” symbol, a decimal point, and an exponential part.

This tutorial shows how this function can be used for different things in PHP.

Is_numeric array PHP?

With a numeric array, we can store more than one value of the same type in a single variable instead of making a new variable for each value.

The index, which is always a number in a numeric array, can then be used to get to these values.

Note: The index always starts at 0 by default.

Is a function numeric?

Returns a Boolean value that says whether an expression can be evaluated as a number.

The required expression argument is a Variant with a string or numeric expression.

Summary

To summarize, we now understand exactly how to use is_numeric.

I hope this tutorial has helped you understand the meaning and application of is_numeric in PHP.

Lastly, if you want to learn more about is_numeric in PHP, please leave a comment below. We’ll be happy to hear it!

Common use cases for How to Check a Value Is Numeric

  • Web application development. Full-stack PHP apps using vanilla PHP or Laravel/Symfony frameworks.
  • WordPress plugin/theme development. Custom functionality for the world’s most popular CMS.
  • API development. REST or GraphQL endpoints serving mobile apps and SPAs.
  • CLI tools. Command-line scripts for cron jobs, data migration, or automation.
  • Legacy code maintenance. PHP powers a large share of the web; understanding it is a durable skill.

Working code example

<?php
declare(strict_types=1);

class UserService {
    public function getGreeting(string $name): string {
        if ($name === "") {
            throw new InvalidArgumentException("Name is required");
        }
        return "Welcome, " . htmlspecialchars($name);
    }
}

$service = new UserService();
echo $service->getGreeting("Alice");
?>

Best practices

  • Enable strict types. declare(strict_types=1) at the top of every file catches type coercion bugs.
  • Use Composer. Modern PHP uses Composer for dependency management, autoloading, and PSR-4 class naming.
  • Follow PSR standards. PSR-12 for coding style, PSR-4 for autoloading, PSR-3 for logging.
  • Write unit tests with PHPUnit. Aim for 70%+ code coverage on business-critical modules.
  • Use static analysis. PHPStan or Psalm catch many bugs before code runs.

Common pitfalls

  • Global state. Overusing global variables makes testing hard. Prefer dependency injection.
  • SQL concatenation. Always use prepared statements. Never concatenate user input into SQL strings.
  • Missing type declarations. Old PHP allowed loose types. Modern PHP encourages strict typing everywhere.
  • Ignoring errors. Set error_reporting(E_ALL) in development. Handle errors explicitly in production.

Debugging PHP code effectively

  • var_dump(). Prints variable type and value. Use during development to inspect state.
  • error_log(). Write to the PHP error log without polluting the response. Best for production debugging.
  • Xdebug. Set breakpoints in VS Code or PhpStorm for step-through debugging.
  • Enable strict error reporting. In development, set error_reporting(E_ALL) and display_errors=On.
  • Log stack traces. In catch blocks, log $e->getTraceAsString() to reproduce complex bugs.

Where to go next after this tutorial

  • Learn a framework. Laravel is the most popular PHP framework in 2026. Symfony is the enterprise choice.
  • Study Composer. Modern PHP relies on Composer for autoloading and dependencies. Learn PSR-4.
  • Practice with real projects. Browse itsourcecode.com PHP Projects for 300+ capstone-ready systems.
  • Read official docs. The PHP manual at php.net is the authoritative reference. Bookmark it.
  • Join the PHP community. Reddit r/PHP, Stack Overflow PHP tag, PHP-FIG for standards.

Related PHP concepts to explore

  • Type declarations. Parameter, return, and property types improve reliability.
  • Namespaces. Prevent function and class name collisions across large codebases.
  • Interfaces and traits. Cornerstone of PHP object-oriented design.
  • Exception handling. try/catch/finally with typed catch blocks (PHP 8+).
  • Enums (PHP 8.1+). Type-safe fixed set of values, replacing constants.

Frequently Asked Questions

What PHP version does this tutorial target?
This tutorial is written for PHP 8.0 or higher. Modern features (arrow functions, named arguments, match expressions, enums, nullsafe operator) work best in PHP 8.1+. For legacy PHP 7.x, most examples still run but with fallback syntax.
Do I need XAMPP to run PHP code examples?
For beginners, XAMPP (Apache + PHP + MySQL) is the easiest setup on Windows. On Mac, use MAMP or Homebrew php. On Linux, install php-cli via apt or yum. For quick one-off tests, use an online PHP sandbox like PHP Sandbox or 3v4l.org.
How do I test the code snippets in this tutorial?
Save each example as a .php file inside XAMPP htdocs folder, start Apache in XAMPP Control Panel, then open http://localhost/yourfile.php in a browser. For pure PHP CLI code, run php yourfile.php from the terminal.
Can I use this in a Laravel project?
Yes. Most native PHP functions covered in these tutorials work identically inside Laravel. Some Laravel helpers (str_helpers, arr_helpers) provide framework-specific wrappers around the same functions.
Where can I get more PHP practice projects?
Browse itsourcecode.com PHP Projects for 300+ free capstone-ready systems (POS, inventory, hospital management, e-commerce). Each includes source code, database SQL, and installation guide for BSIT capstone students.

Leave a Comment