| PHP - Type Casting |
As I already mentioned, you do not need to specify a type when you create a variable, but that doesn't mean the variables do not have types associated with them. You can explicitly set the type, known as type casting, by using the C-style syntax in which you put the type you want in brackets before the variable or expression. For example:
$var = (int)"123abc";
Without the (int) in this example, PHP creates a string variable. With the explicit cast, however, we have created an integer variable with a value of 123. The following table shows the available cast operators in PHP.
|
Operators |
Function |
|---|---|
|
(int), (integer) |
Cast to an integer |
|
(real), (double), (float) |
Cash to a floating point number |
|
(string) |
Cast to a string |
|
(array) |
Cast to an array |
|
(object) |
Cast to an object |
|
(bool), (boolean) |
Cast to a boolean |
|
(unset) |
Cast to NULL; the same as calling unset( ) on the value |
| Users' Comments (0) |
|
No comment posted








