Getting Detailed Information About Variables


The values of variables can be sent to the client using print() or echo(); however, this is sometimes problematic. Take Booleans, for instance. echo(true) prints 1, but echo(false) prints nothing. A much better way is to use var_dump(), a function that also prints the type of the variable. Therefore, this code returns the string bool(false).

This also works for objects and arrays, making var_dump() a must-have option for developers who like to debug without a debugger.

var_dump function syntax

void var_dump ( mixed $expression [, mixed $... ] )

This function displays structured information about one or more expressions that includes its type and value. Arrays and objects are explored recursively with values indented to show structure. This function accepts multiple parameters including objects, variables, array etc.

var_export function syntax

mixed var_export ( mixed $expression [, bool $return = false ] )

var_export() is similar to var_dump(), it displays structured information about the given variable and accepts two parameters:

  1. expression a variable

  2. return returns the variable representation instead of outputing it if true

This function works similarly; however, there are two differences:

  • The return value of var_export() is PHP code; for instance, var_export(false) returns false.

  • If the second parameter provided to var_export() is the Boolean true, the function does not print anything, but returns a string.