Concept

How can PHP values and variable states be converted and checked explicitly?

ComputerScienceOne / Type Juggling

"Relying on type juggling to convert values can be ugly and error prone. You can write much more intentional code by using the several conversion functions provided by PHP. For example:\n\n$a = intval(\"10\");\n\n$b = floatval(\"3.14\");\n\n$c = intval(\"ten\"); //c has the value zero\n\nIn all three of the examples above, the strings are converted just as they are when type juggled. However, the variables are guaranteed to have the type indicated (integer or floating point number).\n\nThere are several utility functions that can be used to help determine the type of variable. The function is_numeric($x) returns true if $x is a numeric (integer or floating point number) or represents a pure numeric string. The functions is_int($x) and is_float($x) each return true or false depending on whether or not $x is of that type.\n\n$a = 10;\n\n$b = \"10\";\n\n$c = 3.14;\n\n$d = \"3.14\";\n\n$e = \"hello\";\n\n$f = \"10foo\";\n\nis_numeric($a); //true\n\nis_numeric($b); //true\n\nis_numeric($c); //true\n\nis_numeric($d); //true\n\nis_numeric($e); //false\n\nis_numeric($f); //false\n\nis_int($a); //true\n\nis_int($b); //false\n\nis_int($c); //false\n\nis_int($d); //false\n\nis_int($e); //false\n\nis_int($f); //false\n\nis_float($a); //false\n\nis_float($b); //false\n\nis_float($c); //true\n\nis_float($d); //false\n\nis_float($e); //false\n\nis_float($f); //false\n\nA more general way to determine the type of a variable is to use the function gettype($x) which returns a string representation of the type of the variable $x. The string returned by this function is one of the following depending on the type of $x: \"boolean\", \"integer\", \"double\", \"string\", \"array\", \"object\", \"resource\", \"NULL\", or \"unknown type\". Other checker functions allow you to determine if a variable has been set, if its null, “empty” etc. For example, is_null($x) returns true if $x is not set or is set, but has been set to null. The function isset($x) returns true only if $x is set and it is not null. The function empty($x) returns true if $x represents an empty entity: an empty string, false, an empty array, null, \"0\", 0, or an unset variable.\n\nResults for various variable values:\n\nValue of $var | isset($var) | empty($var) | is_null($var)\n\n42 | bool(true) | bool(false) | bool(false)\n\n\"\" (an empty string) | bool(true) | bool(true) | bool(false)\n\n\" \" (space) | bool(true) | bool(false) | bool(false)\n\nfalse | bool(true) | bool(true) | bool(false)\n\ntrue | bool(true) | bool(false) | bool(false)\n\narray() (an empty array) | bool(true) | bool(true) | bool(false)\n\nnull | bool(false) | bool(true) | bool(true)\n\n\"0\" (0 as a string) | bool(true) | bool(true) | bool(false)\n\n0 (0 as an integer) | bool(true) | bool(true) | bool(false)\n\n0.0 (0 as a float) | bool(true) | bool(true) | bool(false)\n\nvar $var; (declared with no value) | bool(false) | bool(true) | bool(true)\n\nNULL byte (\"\\0\") | bool(true) | bool(false) | bool(false)"

Related Ideas

How can PHP values and variable states be converted and checked explicitly? | ComputerScienceOne | Bifalgorithm | Bifalgorithm