Sonntag, 1. Juni 2014

PHP Basics: How can I convert from "int" to "bool", "float", "string", "array", "object"

converting variables using "int", "bool", "float", "string", "array", "object". There are several ways to cast a variable into a different variable type. This is useful in order to control what type a variable currently is. there are specific cast commands, yet you can also assign the operators in order to make sure the variable behaves as expected
$k = 4; //this will initialize the variable and set it up to be an integer
$l = (float) $k; // this converts the variable to a float
var_dump($l);

The Output is "float(4)", since the variable was converted from integer to float

Be aware that even tough integer variables can hold significant data of up to 2147483647 on a 32-Bit system, therefor they can run into overload.

1 Kommentar: