As we know PHP does not require or support explicit type definition in variable declaration. However, you can force a variable to be evaluated as a certain type using type casting.
Let's try out the following example to understand how this works:
You can use (int) or (integer) to cast a variable to integer, use (float), (double) or (real) to cast a variable to float. Similarly, you can use the (string) to cast a variable to string, and so on.
The PHP gettype() function returns "double" in case of a float for historical reasons.
Alternatively, you can also use the intval() function to get the integer value of a variable.
Use Type Casting
As we know PHP does not require or support explicit type definition in variable declaration. However, you can force a variable to be evaluated as a certain type using type casting.
Let's try out the following example to understand how this works:
You can use (int) or (integer) to cast a variable to integer, use (float), (double) or (real) to cast a variable to float. Similarly, you can use the (string) to cast a variable to string, and so on.
The PHP gettype() function returns "double" in case of a float for historical reasons.
Alternatively, you can also use the intval() function to get the integer value of a variable.
need an explanation for this answer? contact us directly to get an explanation for this answer