What is the is_integer function in PHP

Check whether a variable is an integer (Theme: PHP examples)

1. is_int ()

The is_int ($ var) function returns whether the transferred variable is an integer:

PHP code <?php var_dump( is_int(1) ); // true var_dump( is_int(-1) ); // true var_dump( is_int(0) ); // true var_dump( is_int(1.0) ); // false var_dump( is_int('1') ); // false var_dump( is_int(true) ); // false var_dump( is_int(new stdClass()) ); // false ?>
HTML code: output bool (true) bool (true) bool (true) bool (false) bool (false) bool (false) bool (false)

2. Test for integers for function parameters

It makes sense to use is_int () to check function parameters:

PHP code <?php function echoQuadrat($zahl) { if (!is_int($zahl)) { echo("Uebergebene Zahl ist kein Integer. Nur Integer sollen durch diese Funktion quadriert werden.\n"); } else { echo("Quadrat von $zahl: ".($zahl*$zahl)."\n"); } } echoQuadrat(10); echoQuadrat(4); echoQuadrat("hallo"); echoQuadrat(false); ?>
HTML code: output Square of 10: 100 Square of 4: 16 The transferred number is not an integer. Only integers should be squared by this function. The number passed is not an integer. Only integers should be squared by this function.