Jump to content

is_numeric question


fubowl

Recommended Posts

Taken from http://php.net/manual/en/function.is-numeric.php

 

<?php
$tests = array(
        "42", 
        1337, 
        "1e4", 
        "not numeric", 
        Array(), 
        9.1
        );

foreach ($tests as $element) {
    if (is_numeric($element)) {
        echo "'{$element}' is numeric", PHP_EOL;
    } else {
        echo "'{$element}' is NOT numeric", PHP_EOL;
    }
}
?>

 

Outputs:

'42' is numeric

'1337' is numeric

'1e4' is numeric

'not numeric' is NOT numeric

'Array' is NOT numeric

'9.1' is numeric

 

WHY does 1e4 validate as TRUE using is_numeric?

 

Link to comment
https://forums.phpfreaks.com/topic/217444-is_numeric-question/
Share on other sites

filter_var

 

$options['options']['min_range'] = 0;
$options['options']['max_range'] = 1337000;
$options['options']['default']   = 0;

if ( filter_var($intVar, FILTER_VALIDATE_INT) )
{
   // valid integer between 0 and 1337000
}
else
{
   // filter failed and returned 0
}

Link to comment
https://forums.phpfreaks.com/topic/217444-is_numeric-question/#findComment-1128987
Share on other sites

  • 4 months later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.