This site is mobile accessible. Press the "Tap Here" button to use a different font-size.
Smartphone icons created by Freepik - Flaticon
In this chapter we will look in depth into Integers, Floats, and Number Strings.
Check if the type of a variable is integer:
<?php $x = 5985; var_dump(is_int($x)); $x = 59.85; var_dump(is_int($x)); ?>
Check if the type of a variable is float:
<?php $x = 10.365; var_dump(is_float($x)); ?>
Check if a numeric value is finite or infinite:
<?php $x = 1.9e411; var_dump($x); ?>
Invalid calculation will return a NaN value:
<?php $x = acos(8); var_dump($x); ?>
The PHP is_numeric() function can be used to find whether a variable is numeric. The function returns true if the variable is a number or a numeric string, false otherwise.
Check if the variable is numeric:
<?php $x = 5985; var_dump(is_numeric($x)); $x = "5985"; var_dump(is_numeric($x)); $x = "59.85" + 100; var_dump(is_numeric($x)); $x = "Hello"; var_dump(is_numeric($x)); ?>
Note: From PHP 7.0: The is_numeric() function will return FALSE for numeric strings in hexadecimal form (e.g. 0xf4c3b00c), as they are no longer considered as numeric strings.
Cast float and string to integer:
<?php // Cast float to int $x = 23465.768; $int_cast = (int)$x; echo $int_cast; echo "<br>"; // Cast string to int $x = "23465.768"; $int_cast = (int)$x; echo $int_cast; ?>
Eventually the navigation links, above, will be replaced by these (previous) and (next) buttons below.
Animated PHP icons used in the buttons provided by ICONS8.COM. Smartphone icons created by Freepik - Flaticon
Module 2 quiz