PHP: Hypertext Preprocessor (original) (raw)

is_int

(PHP 4, PHP 5, PHP 7, PHP 8)

is_int — Find whether the type of a variable is integer

Description

Note:

To test if a variable is a number or a numeric string (such as form input, which is always a string), you must useis_numeric().

Parameters

value

The variable being evaluated.

Return Values

Returns [true](reserved.constants.php#constant.true) if value is an int,[false](reserved.constants.php#constant.false) otherwise.

Examples

Example #1 is_int() example

<?php $values = array(23, "23", 23.5, "23.5", null, true, false); foreach ($values as $value) { echo "is_int("; var_export($value); echo ") = "; var_dump(is_int($value)); } ?>

The above example will 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)

See Also

Found A Problem?

Simon Neaves

17 years ago

`I've found that both that is_int and ctype_digit don't behave quite as I'd expect, so I made a simple function called isInteger which does. I hope somebody finds it useful.

`

Robin

15 years ago

`Keep in mind that is_int() operates in signed fashion, not unsigned, and is limited to the word size of the environment php is running in.

In a 32-bit environment:

In a 64-bit environment:

If you find yourself deployed in a 32-bit environment where you are required to deal with numeric confirmation of integers (and integers only) potentially breaching the 32-bit span, you can combine is_int() with is_float() to guarantee a cover of the full, signed 64-bit span:

small=2147483647;//willalwaysbetrueforisint(),butneverforisfloat()<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>b</mi><mi>i</mi><mi>g</mi><mo>=</mo><mn>9223372036854775807</mn><moseparator="true">;</mo><mimathvariant="normal">/</mi><mimathvariant="normal">/</mi><mi>w</mi><mi>i</mi><mi>l</mi><mi>l</mi><mi>o</mi><mi>n</mi><mi>l</mi><mi>y</mi><mi>b</mi><mi>e</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>e</mi><mi>f</mi><mi>o</mi><mi>r</mi><mi>i</mi><msub><mi>s</mi><mi>i</mi></msub><mi>n</mi><mi>t</mi><mostretchy="false">(</mo><mostretchy="false">)</mo><mi>i</mi><mi>n</mi><mi>a</mi><mn>64</mn><mo>−</mo><mi>b</mi><mi>i</mi><mi>t</mi><mi>e</mi><mi>n</mi><mi>v</mi><mi>i</mi><mi>r</mi><mi>o</mi><mi>n</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mi>i</mi><mi>f</mi><mostretchy="false">(</mo><mi>i</mi><msub><mi>s</mi><mi>i</mi></msub><mi>n</mi><mi>t</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/x−tex">big=9223372036854775807;//willonlybetrueforisint()ina64−bitenvironmentif(isint(</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.8889em;vertical−align:−0.1944em;"></span><spanclass="mordmathnormal">bi</span><spanclass="mordmathnormal"style="margin−right:0.03588em;">g</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="margin−right:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;vertical−align:−0.25em;"></span><spanclass="mord">9223372036854775807</span><spanclass="mpunct">;</span><spanclass="mspace"style="margin−right:0.1667em;"></span><spanclass="mord">//</span><spanclass="mordmathnormal"style="margin−right:0.02691em;">w</span><spanclass="mordmathnormal">i</span><spanclass="mordmathnormal"style="margin−right:0.01968em;">ll</span><spanclass="mordmathnormal">o</span><spanclass="mordmathnormal">n</span><spanclass="mordmathnormal"style="margin−right:0.01968em;">l</span><spanclass="mordmathnormal"style="margin−right:0.03588em;">y</span><spanclass="mordmathnormal">b</span><spanclass="mordmathnormal">e</span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal"style="margin−right:0.02778em;">r</span><spanclass="mordmathnormal">u</span><spanclass="mordmathnormal">e</span><spanclass="mordmathnormal"style="margin−right:0.10764em;">f</span><spanclass="mordmathnormal"style="margin−right:0.02778em;">or</span><spanclass="mordmathnormal">i</span><spanclass="mord"><spanclass="mordmathnormal">s</span><spanclass="msupsub"><spanclass="vlist−tvlist−t2"><spanclass="vlist−r"><spanclass="vlist"style="height:0.3117em;"><spanstyle="top:−2.55em;margin−left:0em;margin−right:0.05em;"><spanclass="pstrut"style="height:2.7em;"></span><spanclass="sizingreset−size6size3mtight"><spanclass="mordmathnormalmtight">i</span></span></span></span><spanclass="vlist−s">​</span></span><spanclass="vlist−r"><spanclass="vlist"style="height:0.15em;"><span></span></span></span></span></span></span><spanclass="mordmathnormal">n</span><spanclass="mordmathnormal">t</span><spanclass="mopen">(</span><spanclass="mclose">)</span><spanclass="mordmathnormal">ina</span><spanclass="mord">64</span><spanclass="mspace"style="margin−right:0.2222em;"></span><spanclass="mbin">−</span><spanclass="mspace"style="margin−right:0.2222em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;vertical−align:−0.25em;"></span><spanclass="mordmathnormal">bi</span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal">e</span><spanclass="mordmathnormal">n</span><spanclass="mordmathnormal"style="margin−right:0.03588em;">v</span><spanclass="mordmathnormal">i</span><spanclass="mordmathnormal">ro</span><spanclass="mordmathnormal">nm</span><spanclass="mordmathnormal">e</span><spanclass="mordmathnormal">n</span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal">i</span><spanclass="mordmathnormal"style="margin−right:0.10764em;">f</span><spanclass="mopen">(</span><spanclass="mordmathnormal">i</span><spanclass="mord"><spanclass="mordmathnormal">s</span><spanclass="msupsub"><spanclass="vlist−tvlist−t2"><spanclass="vlist−r"><spanclass="vlist"style="height:0.3117em;"><spanstyle="top:−2.55em;margin−left:0em;margin−right:0.05em;"><spanclass="pstrut"style="height:2.7em;"></span><spanclass="sizingreset−size6size3mtight"><spanclass="mordmathnormalmtight">i</span></span></span></span><spanclass="vlist−s">​</span></span><spanclass="vlist−r"><spanclass="vlist"style="height:0.15em;"><span></span></span></span></span></span></span><spanclass="mordmathnormal">n</span><spanclass="mordmathnormal">t</span><spanclass="mopen">(</span></span></span></span>small)∣∣isfloat(small = 2147483647; // will always be true for is_int(), but never for is_float() <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>b</mi><mi>i</mi><mi>g</mi><mo>=</mo><mn>9223372036854775807</mn><mo separator="true">;</mo><mi mathvariant="normal">/</mi><mi mathvariant="normal">/</mi><mi>w</mi><mi>i</mi><mi>l</mi><mi>l</mi><mi>o</mi><mi>n</mi><mi>l</mi><mi>y</mi><mi>b</mi><mi>e</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>e</mi><mi>f</mi><mi>o</mi><mi>r</mi><mi>i</mi><msub><mi>s</mi><mi>i</mi></msub><mi>n</mi><mi>t</mi><mo stretchy="false">(</mo><mo stretchy="false">)</mo><mi>i</mi><mi>n</mi><mi>a</mi><mn>64</mn><mo>−</mo><mi>b</mi><mi>i</mi><mi>t</mi><mi>e</mi><mi>n</mi><mi>v</mi><mi>i</mi><mi>r</mi><mi>o</mi><mi>n</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mi>i</mi><mi>f</mi><mo stretchy="false">(</mo><mi>i</mi><msub><mi>s</mi><mi>i</mi></msub><mi>n</mi><mi>t</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">big = 9223372036854775807; // will only be true for is_int() in a 64-bit environmentif( is_int(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">bi</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord">9223372036854775807</span><span class="mpunct">;</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord">//</span><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mord mathnormal">i</span><span class="mord mathnormal" style="margin-right:0.01968em;">ll</span><span class="mord mathnormal">o</span><span class="mord mathnormal">n</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal" style="margin-right:0.03588em;">y</span><span class="mord mathnormal">b</span><span class="mord mathnormal">e</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">u</span><span class="mord mathnormal">e</span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span><span class="mord mathnormal">i</span><span class="mord"><span class="mord mathnormal">s</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3117em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">i</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">n</span><span class="mord mathnormal">t</span><span class="mopen">(</span><span class="mclose">)</span><span class="mord mathnormal">ina</span><span class="mord">64</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">bi</span><span class="mord mathnormal">t</span><span class="mord mathnormal">e</span><span class="mord mathnormal">n</span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mord mathnormal">i</span><span class="mord mathnormal">ro</span><span class="mord mathnormal">nm</span><span class="mord mathnormal">e</span><span class="mord mathnormal">n</span><span class="mord mathnormal">t</span><span class="mord mathnormal">i</span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mopen">(</span><span class="mord mathnormal">i</span><span class="mord"><span class="mord mathnormal">s</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3117em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">i</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">n</span><span class="mord mathnormal">t</span><span class="mopen">(</span></span></span></span>small) || is_float(small=2147483647;//willalwaysbetrueforisint(),butneverforisfloat()<spanclass="katex"><spanclass="katexmathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>b</mi><mi>i</mi><mi>g</mi><mo>=</mo><mn>9223372036854775807</mn><moseparator="true">;</mo><mimathvariant="normal">/</mi><mimathvariant="normal">/</mi><mi>w</mi><mi>i</mi><mi>l</mi><mi>l</mi><mi>o</mi><mi>n</mi><mi>l</mi><mi>y</mi><mi>b</mi><mi>e</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>e</mi><mi>f</mi><mi>o</mi><mi>r</mi><mi>i</mi><msub><mi>s</mi><mi>i</mi></msub><mi>n</mi><mi>t</mi><mostretchy="false">(</mo><mostretchy="false">)</mo><mi>i</mi><mi>n</mi><mi>a</mi><mn>64</mn><mo></mo><mi>b</mi><mi>i</mi><mi>t</mi><mi>e</mi><mi>n</mi><mi>v</mi><mi>i</mi><mi>r</mi><mi>o</mi><mi>n</mi><mi>m</mi><mi>e</mi><mi>n</mi><mi>t</mi><mi>i</mi><mi>f</mi><mostretchy="false">(</mo><mi>i</mi><msub><mi>s</mi><mi>i</mi></msub><mi>n</mi><mi>t</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/xtex">big=9223372036854775807;//willonlybetrueforisint()ina64bitenvironmentif(isint(</annotation></semantics></math></span><spanclass="katexhtml"ariahidden="true"><spanclass="base"><spanclass="strut"style="height:0.8889em;verticalalign:0.1944em;"></span><spanclass="mordmathnormal">bi</span><spanclass="mordmathnormal"style="marginright:0.03588em;">g</span><spanclass="mspace"style="marginright:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="marginright:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;verticalalign:0.25em;"></span><spanclass="mord">9223372036854775807</span><spanclass="mpunct">;</span><spanclass="mspace"style="marginright:0.1667em;"></span><spanclass="mord">//</span><spanclass="mordmathnormal"style="marginright:0.02691em;">w</span><spanclass="mordmathnormal">i</span><spanclass="mordmathnormal"style="marginright:0.01968em;">ll</span><spanclass="mordmathnormal">o</span><spanclass="mordmathnormal">n</span><spanclass="mordmathnormal"style="marginright:0.01968em;">l</span><spanclass="mordmathnormal"style="marginright:0.03588em;">y</span><spanclass="mordmathnormal">b</span><spanclass="mordmathnormal">e</span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal"style="marginright:0.02778em;">r</span><spanclass="mordmathnormal">u</span><spanclass="mordmathnormal">e</span><spanclass="mordmathnormal"style="marginright:0.10764em;">f</span><spanclass="mordmathnormal"style="marginright:0.02778em;">or</span><spanclass="mordmathnormal">i</span><spanclass="mord"><spanclass="mordmathnormal">s</span><spanclass="msupsub"><spanclass="vlisttvlistt2"><spanclass="vlistr"><spanclass="vlist"style="height:0.3117em;"><spanstyle="top:2.55em;marginleft:0em;marginright:0.05em;"><spanclass="pstrut"style="height:2.7em;"></span><spanclass="sizingresetsize6size3mtight"><spanclass="mordmathnormalmtight">i</span></span></span></span><spanclass="vlists"></span></span><spanclass="vlistr"><spanclass="vlist"style="height:0.15em;"><span></span></span></span></span></span></span><spanclass="mordmathnormal">n</span><spanclass="mordmathnormal">t</span><spanclass="mopen">(</span><spanclass="mclose">)</span><spanclass="mordmathnormal">ina</span><spanclass="mord">64</span><spanclass="mspace"style="marginright:0.2222em;"></span><spanclass="mbin"></span><spanclass="mspace"style="marginright:0.2222em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;verticalalign:0.25em;"></span><spanclass="mordmathnormal">bi</span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal">e</span><spanclass="mordmathnormal">n</span><spanclass="mordmathnormal"style="marginright:0.03588em;">v</span><spanclass="mordmathnormal">i</span><spanclass="mordmathnormal">ro</span><spanclass="mordmathnormal">nm</span><spanclass="mordmathnormal">e</span><spanclass="mordmathnormal">n</span><spanclass="mordmathnormal">t</span><spanclass="mordmathnormal">i</span><spanclass="mordmathnormal"style="marginright:0.10764em;">f</span><spanclass="mopen">(</span><spanclass="mordmathnormal">i</span><spanclass="mord"><spanclass="mordmathnormal">s</span><spanclass="msupsub"><spanclass="vlisttvlistt2"><spanclass="vlistr"><spanclass="vlist"style="height:0.3117em;"><spanstyle="top:2.55em;marginleft:0em;marginright:0.05em;"><spanclass="pstrut"style="height:2.7em;"></span><spanclass="sizingresetsize6size3mtight"><spanclass="mordmathnormalmtight">i</span></span></span></span><spanclass="vlists"></span></span><spanclass="vlistr"><spanclass="vlist"style="height:0.15em;"><span></span></span></span></span></span></span><spanclass="mordmathnormal">n</span><spanclass="mordmathnormal">t</span><spanclass="mopen">(</span></span></span></span>small)∣∣isfloat(small) ); // passes in a 32-bit environment if( is_int($big) || is_float($big) ); // passes in a 32-bit environment ?>

`

e dot sand at elisand dot com

16 years ago

`Simon Neaves was close on explaining why his function is perfect choice for testing for an int (as possibly most people would need). He made some errors on his ctype_digit() output though - possibly a typo, or maybe a bug in his version of PHP at the time.

The correct output for parts of his examples should be:

As you can see, the reason why using just ctype_digit() may not always work is because it only returns TRUE when given a string as input - given a number value and it returns FALSE (which may be unexpected).

`

andre dot roesti at 7flex dot net

14 years ago

`With this function you can check if every of multiple variables are int. This is a little more comfortable than writing 'is_int' for every variable you've got.

argsasargs as argsasarg ) if ( ! is_int ( $arg ) ) return false; return true; }// Example: are_int ( 4, 9 ); // true are_int ( 22, 08, 'foo' ); // false ?>

`

davide dot renzi at gmail dot com

9 years ago

`I've found a faster way of determining an integer.
On my env, this method takes about half the time of using is_int().

Cast the value then check if it is identical to the original.

n!==n !== n!==n ) { echo 'not is int'; } else { echo 'is int'; } ?>

`

petepostma at gmail dot spam dot com

12 years ago

`There is a versa to the vice of this int only type check.

is_int( $integer_type) will only return true, if the TYPE is int, not the value
ctype_digit( $string_type) will only return true if the TYPE is string, and its value is INT

therefore:
return ( is_int($value) || ctype_digit($value) );

`

nicolas dot giraud at actiane dot com

12 years ago

`Just a shorter way to check if your variable is an int or a string containing a int without others digit than 0 to 9 :

bool=(!isint(bool = ( !is_int(bool=(!isint(value) ? (ctype_digit($value)) : true );$value = 42; //true $value = '42'; //true $value = '1e9'; //false $value = '0155'; //true $value = 0155; //true $value = 0xFF; //true while it's just the same as 255 $value = '0xFF'; //false $value = 'a'; //false $value = array(); //false $value = array('5'); //false $value = array(5); false $value = ''; //false $value = NULL; //false?>

Short & cool :)

`

Vasiliy Makogon

9 years ago

`If you want detect integer of float values, which presents as pure int or float, and presents as string values, use this functions:

PHP_INT_MAX) { return false; } return is_float($val) ? false : preg_match('~^((:?+|-)?[0-9]+)$~', $val); } function isFloat($val) { if (!is_scalar($val)) { return false; } return is_float($val + 0); } foreach ([ '11111111111111111', 11111111111111111, // > PHP_INT_MAX - presents in PHP as float 1, '10', '+1', '1.1', 1.1, .2, 2., '.2', '2.', '-2.', '-.2', null, [], true, false, 'string' ] as $value) { echo value.′:′.gettype(value . ':' . gettype(value.:.gettype(value) . ' is Integer? - ' . (isInteger($value) ? 'yes' : 'no') . PHP_EOL; echo value.′:′.gettype(value . ':' . gettype(value.:.gettype(value) . ' is Float? - ' . (isFloat($value) ? 'yes' : 'no') . PHP_EOL; } ?>

`

me at rexxars dot com

15 years ago

`I was looking for the fastest way to check for an unsigned integer which supported large numbers like 4318943448871348 or 0xFFFFFFFF.

Fastest I came up with is this:

Will return true on 1515, 0xFFFFFFFF, '3515' and '1365158185855141'.
Will return false on 0.1515, '415.4134' and '-616'.

Be aware though, before PHP 5.1.0 this will return true on an empty string.

According to my benchmarks this is about 30% faster than the regex ^\d+$.

`

gabe at websaviour dot com

21 years ago

`Although this can be inferred from the documentation, beware of numeric strings. I ran into the problem in a MySQL app where I would either SELECT an INT PRIMARY KEY or INSERT a new record and use mysql_insert_id() to get the KEY before continuing onto the new section.

I used is_int() to make sure the subsequent queries wouldn't break when using the key variable. Unfortunately I failed to realize that while mysql_insert_id() returns an int, mysql_result() always returns a string even if you are SELECTing from an INT field.

Spent at least 30 minutes trying to figure out why existing records weren't getting linked, but new records would link fine. I ended up using intval() on mysql_result() to make sure subsequent queries still always work.

`