Php if variable is an integer

How to check if a variable is an integer or number in PHP: A comprehensive guide

Learn how to check if a variable is an integer or number in PHP with built-in functions like is_int() and is_numeric(), and conditions like ctype_digit(). Discover the difference between Integer and int in PHP. Improve your code today!

  • Introduction
  • is_int() Function
  • is_numeric() Function
  • Checking if a Variable is a Whole Number
  • Other Functions to Check the Type of a Variable
  • Difference between Integer and int in PHP
  • Other helpful code examples for checking if a PHP variable is an integer or number
  • Conclusion
  • How do you check if a variable is an integer?
  • How to use Is_numeric in PHP?
  • What is the difference between int and integer in PHP?
  • How to check if a string can be converted to int PHP?

If you are a PHP developer, you must have come across situations where you need to check if a variable is an integer or a number. Luckily, PHP provides several built-in functions that make it easy to check the type of a variable. In this guide, we will walk you through the different functions in PHP that can be used to check if a variable is an integer or a number.

Introduction

Before we dive into the different functions in PHP that can be used to check the type of a variable, let’s first explain what the user intent is. If you are reading this article, you are probably seeking information on how to check if a variable is an integer or a number in PHP. In this guide, we will provide a comprehensive overview of the different functions that you can use to achieve this.

is_int() Function

The first function we will look at is the is_int() function. This is a built-in PHP function that can be used to test whether the type of the specified variable is an integer or not. Here’s an example of how to use the is_int() function:

In this example, we have assigned the value 5 to the variable $var . We then use the is_int() function to test if $var is an integer. The function returns TRUE if the variable is an integer, and FALSE otherwise.

is_numeric() Function

The second function we will look at is the is_numeric() function. This is another built-in PHP function that checks whether a variable is a number or a numeric string. Here’s an example of how to use the is_numeric() function:

$var = "3.14"; if (is_numeric($var)) < echo "Variable is a number or numeric string"; >else

In this example, we have assigned the value «3.14» to the variable $var . We then use the is_numeric() function to test if $var is a number or a numeric string. The function returns TRUE if the variable is a number or a numeric string, and FALSE otherwise.

Читайте также:  Вывод нескольких переменных питон

It’s worth noting that using is_numeric() to check if a variable is an integer is not recommended because it will return TRUE for numeric strings like «3.14» .

Checking if a Variable is a Whole Number

To check if a variable is a whole number, you can use a condition like ($var == (string)(int)$var) or ctype_digit($var) . Here are examples of how to use these conditions to check if a variable is a whole number:

$var = 10; if ($var == (string)(int)$var) < echo "Variable is a whole number"; >else
$var = "10"; if (ctype_digit($var)) < echo "Variable is a whole number"; >else

In the first example, we convert $var to an integer using (int)$var . We then convert it back to a string using (string)(int)$var . If the original value of $var was a whole number, the string value will be the same as the original value. We then compare the string value to the original value using the == operator.

In the second example, we use the ctype_digit() function to check if the variable $var contains only digits.

Other Functions to Check the Type of a Variable

Apart from is_int() and is_numeric() , there are other functions that you can use to check the type of a variable in PHP. These include gettype() , is_string() , is_float() , isset() , and empty() . Here’s a brief explanation of each function:

  • gettype() : Returns the type of the specified variable.
  • is_string() : Checks whether the type of the specified variable is a string.
  • is_float() : Checks whether the type of the specified variable is a float.
  • isset() : Checks whether a variable is set and is not NULL.
  • empty() : Checks whether a variable is empty.

Here’s an example of how to use the gettype() function:

This will output integer , which is the type of the variable $var .

Difference between Integer and int in PHP

Before we wrap up this guide, let’s explain the difference between an Integer and an int in PHP. Integer is a wrapper class whereas int is a primitive data type. A wrapper class is a class that encapsulates a primitive data type, whereas a primitive data type is a basic data type that is not defined in terms of other data types.

Here’s an example of how to use Integer and int in PHP:

$var1 = new Integer(5); $var2 = 5;if ($var1 == $var2) < echo "Variables are equal"; >else

In this example, we create an Integer object with a value of 5 and assign it to $var1 . We then assign the integer value 5 to $var2 . We then compare the two variables using the == operator. Since Integer is a wrapper class, we can use the == operator to compare it with an int value.

Other helpful code examples for checking if a PHP variable is an integer or number

In Php as proof, php is variable a number code example

Читайте также:  Not moving background css

In Php , php check if variable is int code example

// Check if variable is int $id = "1";if(!intval($id)) < throw new Exception("Not Int", 404); >else< // this variable is int >

In Php , for example, php is int code example

In Php case in point, php is_int code sample

 ?> /* Output is_int(23) = bool(true) is_int('23') = bool(false) is_int(23.5) = bool(false) is_int('23.5') = bool(false) is_int(NULL) = bool(false) is_int(true) = bool(false) is_int(false) = bool(false) */ 

In Php , php check if string is integer code sample

Conclusion

In this guide, we have provided a comprehensive overview of the different functions in PHP that can be used to check if a variable is an integer or a number. We have explained how to use is_int() and is_numeric() functions, how to check if a variable is a whole number, and other functions that can be used to check the type of a variable. We have also explained the difference between Integer and int in PHP. It’s important to choose the right function to check the type of a variable in PHP. We hope this guide has been helpful, and that you can use the tips and examples provided to improve your code.

Источник

is_numeric

Determines if the given variable is a number or a numeric string.

Parameters

The variable being evaluated.

Return Values

Returns true if value is a number or a numeric string, false otherwise.

Changelog

Version Description
8.0.0 Numeric strings ending with whitespace ( «42 » ) will now return true . Previously, false was returned instead.

Examples

Example #1 is_numeric() examples

$tests = array(
«42» ,
1337 ,
0x539 ,
02471 ,
0b10100111001 ,
1337e0 ,
«0x539» ,
«02471» ,
«0b10100111001» ,
«1337e0» ,
«not numeric» ,
array(),
9.1 ,
null ,
» ,
);

foreach ( $tests as $element ) if ( is_numeric ( $element )) echo var_export ( $element , true ) . » is numeric» , PHP_EOL ;
> else echo var_export ( $element , true ) . » is NOT numeric» , PHP_EOL ;
>
>
?>

The above example will output:

'42' is numeric 1337 is numeric 1337 is numeric 1337 is numeric 1337 is numeric 1337.0 is numeric '0x539' is NOT numeric '02471' is numeric '0b10100111001' is NOT numeric '1337e0' is numeric 'not numeric' is NOT numeric array ( ) is NOT numeric 9.1 is numeric NULL is NOT numeric '' is NOT numeric

Example #2 is_numeric() with whitespace

$tests = [
» 42″ ,
«42 » ,
«\u9001» , // non-breaking space
«9001\u» , // non-breaking space
];

foreach ( $tests as $element ) if ( is_numeric ( $element )) echo var_export ( $element , true ) . » is numeric» , PHP_EOL ;
> else echo var_export ( $element , true ) . » is NOT numeric» , PHP_EOL ;
>
>
?>

Output of the above example in PHP 8:

' 42' is numeric '42 ' is numeric ' 9001' is NOT numeric '9001 ' is NOT numeric

Output of the above example in PHP 7:

' 42' is numeric '42 ' is NOT numeric ' 9001' is NOT numeric '9001 ' is NOT numeric

See Also

  • Numeric strings
  • ctype_digit() — Check for numeric character(s)
  • is_bool() — Finds out whether a variable is a boolean
  • is_null() — Finds whether a variable is null
  • is_float() — Finds whether the type of a variable is float
  • is_int() — Find whether the type of a variable is integer
  • is_string() — Find whether the type of a variable is string
  • is_object() — Finds whether a variable is an object
  • is_array() — Finds whether a variable is an array
  • filter_var() — Filters a variable with a specified filter
Читайте также:  Best book about html and css

User Contributed Notes 8 notes

Note that the function accepts extremely big numbers and correctly evaluates them.

$v = is_numeric ( ‘58635272821786587286382824657568871098287278276543219876543’ ) ? true : false ;

var_dump ( $v );
?>

The above script will output:

So this function is not intimidated by super-big numbers. I hope this helps someone.

PS: Also note that if you write is_numeric (45thg), this will generate a parse error (since the parameter is not enclosed between apostrophes or double quotes). Keep this in mind when you use this function.

for strings, it return true only if float number has a dot

is_numeric( ‘42.1’ )//true
is_numeric( ‘42,1’ )//false

Apparently NAN (Not A Number) is a number for the sake of is_numeric().

echo «is » ;
if (! is_numeric ( NAN ))
echo «not » ;
echo «a number» ;
?>

Outputs «is a number». So something that is NOT a number (by defintion) is a number.

is_numeric fails on the hex values greater than LONG_MAX, so having a large hex value parsed through is_numeric would result in FALSE being returned even though the value is a valid hex number

is incorrect for PHP8, it’s numeric.

Note that this function is not appropriate to check if «is_numeric» for very long strings. In fact, everything passed to this function is converted to long and then to a double. Anything greater than approximately 1.8e308 is too large for a double, so it becomes infinity, i.e. FALSE. What that means is that, for each string with more than 308 characters, is_numeric() will return FALSE, even if all chars are digits.

However, this behaviour is platform-specific.

In such a case, it is suitable to use regular expressions:

function is_numeric_big($s=0) return preg_match(‘/^-?\d+$/’, $s);
>

Note that is_numeric() will evaluate to false for number strings using decimal commas.

regarding the global vs. american numeral notations, it should be noted that at least in japanese, numbers aren’t grouped with an extra symbol every three digits, but rather every four digits (for example 1,0000 instead of 10.000). also nadim’s regexen are slightly suboptimal at one point having an unescaped ‘.’ operator, and the whole thing could easily be combined into a single regex (speed and all).

$eng_or_world = preg_match
( ‘/^[+-]?’ . // start marker and sign prefix
‘((((7+)|(2(,4)+)))?(\\.8)?(6*)|’ . // american
‘(((9+)|(4(\\.5)+)))?(,1)?(4*))’ . // world
‘(e8+)?’ . // exponent
‘$/’ , // end marker
$str ) == 1 ;
?>

i’m sure this still isn’t optimal, but it should also cover japanese-style numerals and it fixed a couple of other issues with the other regexen. it also allows for an exponent suffix, the pre-decimal digits are optional and it enforces using either grouped or ungrouped integer parts. should be easier to trim to your liking too.

Источник

Оцените статью