Bifalgorithm post · Computer science one · Type Juggling

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

How can PHP values and variable states be converted and checked explicitly? (Computer science one · Type Juggling) — "Relying on type juggling to convert value…

Ideas in this flowchart

  • PHP provides explicit value-conversion and variable-type checking functions.
  • Relying on type juggling can be ugly and error prone; explicit conversion makes code more intentional.
  • Conversion functions explicitly convert values while guaranteeing the indicated type.
  • intval("10") converts the string to the integer 10.
  • floatval("3.14") converts the string to the floating-point value 3.14.
  • intval("ten") produces the integer value zero.
  • Type-checking utilities determine whether a value is numeric or has a specific type.
  • is_numeric($x) returns true for numbers and pure numeric strings, but false for nonnumeric strings such as "hello" and "10foo".
  • is_int($x) returns true only for integers, while is_float($x) returns true only for floating-point numbers.
  • gettype($x) provides a general type check.
  • gettype($x) returns a string such as "boolean", "integer", "double", "string", "array", "object", "resource", "NULL", or "unknown type".
  • Additional checker functions determine whether a variable is set, null, or empty.
  • is_null($x) returns true when $x is unset or has been set to null.
  • isset($x) returns true only when $x is set and not null.
  • empty($x) returns true for an empty string, false, an empty array, null, "0", 0, or an unset variable.
  • The comparison table shows that whitespace and the NULL byte are not empty, while zero values, false, an empty string, an empty array, null, and an unset variable are empty.
  • For null or an unset variable, isset($var) is false, empty($var) is true, and is_null($var) is true.
Feed/

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

No cover

Channel

Computer science one

1 page

"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)"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.