

This means that PHP implicitly performs the type conversion so that your code doesn’t raise an error. If you have a number that is initialized as a string in your code, PHP allows you to perform arithmetic operations directly on that string variable. Ĭonvert a String to a Number Implicitly using Mathematical Operations Similarly, we can use floatval() to convert to float numbers. Īpart from string to integer conversion, intval() can also be used to convert strings of float numbers to integers. The default base value is 10 (for decimal representations). The intval() function can also use a second parameter that specifies the base for conversion. Intval() can also be used to convert strings from hexadecimal (base 16) and octal (base 8) number systems to integers of decimal representations (base 10). Below are a few examples of intval() ’s usage. To convert a PHP string to a number, we can also use the intval() or floatval() function. For example - Ĭonvert a String to a Number Using intval()

This operation floors the float number (eg. Using (int), we can also convert PHP strings that represent float numbers (eg. Similarly, we can also cast PHP strings to float values using (float). To convert a PHP string to a number, we can perform type casting using (int) or (float) keywords as shown below. acos(x) is undefined for x > 1 and x 1 and xĬonvert a String to a Number Using Type Casting It represents outputs of mathematical operations that can not be defined.

You can verify if a variable is INF by using the is_infinite() or is_finite() function as shown below. INF can also be in the negative form, which can be encountered when you perform an operation like log(0). INF is usually encountered any time you happen to divide an integer or float number by zero. In programming languages, it is commonly used to represent any number that is greater than the maximum possible float value (which is platform-dependent in PHP). You can verify if a variable is a float number by using the is_float() function, as shown below. It is important to note that arithmetic operations performed between a float number and an integer always return a float number (even if the returned number does not need a decimal part). These numbers encompass a higher range of numbers, take up more bytes per number, and are precise up to 14 decimal places. įloat numbers are those that contain a decimal component or are represented in an exponential form. You can verify if a variable is an integer by using the is_int() function, as shown below. If you initialise a variable in PHP as a number that does not have a decimal component, it takes the integer data type (unless it has a value greater than PHP_INT_MAX). Integers are the numbers that do not contain a decimal component. On the other hand, Infinity and NaN are not as well-defined and are more likely to be encountered in edge-cases. Here, integers and floats represent the more commonly used number formats in programming languages, and in everyday life.

Even though PHP helps with implicit type conversion in some cases, it is important to know about appropriate methods that can facilitate type conversion. For example, performing arithmetic operations, responding to a client request, feeding the data to a database etc. When working with programming languages, it is quite common to want to do things with numbers that are represented as strings. This can save you from prospective type errors in your code. PHP implicitly declares a data type for your variable. This means that when initializing a variable in PHP, one doesn’t need to declare the variable type.
