PHP's variable-related functions are a key part of the language. Skilled programmers rely on them extensively to build robust code that uses type-checking. Functions like var_dump() and print_r() are also invaluable when debugging.
Here is a quick, task-based overview of the function group:
-
Cast a value from one type to another: doubleval(), intval(), strval()
-
Set the type of a variable: settype()
-
Determine the type of a variable: is_array(), is_bool(), is_double(), is_float(), is_int(), is_integer(), is_long(), is_null(), is_numeric(), is_object(), is_real(), is_resource(), is_scalar(), is_string(), get_resource_type(), gettype()
-
Destroy variables: unset()
-
Gather information about variables: empty(), get_defined_vars(), isset(), print_r(), var_dump()
-
Serialize and unserialize variables: serialize(), unserialize()
Print the entire $GLOBALS array using PHP's functions for processing associative arrays:<?php reset($GLOBALS); while (list($key, $var) = each($GLOBALS)) { print "$key => $var\ n<br>\ n"; } ?>
The example uses PHP's list functions and each functions to iteratively process the $GLOBALS array. It then prints the keys (names of the variables stored in the $GLOBALS array) and their corresponding values.
PHP debugging
var_dump functions displays information about variables in a simple, readable format. This function is very useful when debugging—providing a simple and easy way to display the current contents of one or more variables.For simple scalar variables such as booleans, integers, strings, and doubles, the type of the variable is printed, followed by an opening bracket, the value contained in the variable, and a closing bracket.