PHP Variable Functions
This article is focusing on PHP functions specifically for handling PHP variables. These functions are used for performing variable related functionalities. And, the following list includes such common functionalities applied on PHP variables.
- Getting the value of PHP variables
- PHP Variable type handling.
- printing functions.
- Importing/Exporting PHP variables.
- Other variable functions.
Since, we have enough basic information about PHP variables, let us directly get into this topic, variable functions, dealing with the above list of operations.
Getting the value of PHP variables.
For simply getting the value of the variable, we can use its name followed by $ sign, and we can use the value for any purpose, like assigning it to another variable, returning it to the browser or for any other thing.
But, PHP includes certain list of variable functions, that are dedicated for the special purposes, (i.e.), for getting specified type value of a PHP variable. There are appropriate functions for each PHP supported datatype, for returning variable values in that particular type. These functions are,
- intval() – function accepts variable reference and base parameters to return integer by converting variable type with respect to the base.
- boolval() – function returns boolean value of a given variable. And, it required only value or variable reference.
- floatval() – returns float value. And similarly, PHP contains an alias function doubleval().
- strval() – returns string value.
- intval() is not applicable for PHP object type data. It will take 10 as a default value for base parameter to return decimal value of the given variable.
- boolval() will return false, if variables contains 0, null or empty string as its value. And also applicable for objects.
PHP Variable Type Handling
- get_resource_type() – returns type of the resource data to indicate, whether it is file resource or database resource or any other thing, and it returns FALSE, while invoking with non-resource data.
- gettype()/settype() – We have seen these functions while learning about PHP datatype conversion, and, these are getters and setters of PHP variable data types.
- When PHP variable function get_resource_type() could not identify the type of resource, it will return string data as unknown.
And, PHP contains some more variable functions behaving with respect to variable types. These functions are most widely used for PHP if/else statement.
These variable functions help to make decision to take or not to take the branch based on the boolean value returned by this function. Let us list out some of such PHP functions below.
- is_numeric() – returns TRUE, if given data contains numeric value.
- is_int(), is_integer(), is_long() – All these functions are used for checking, whether a variable is integer or not based on the size of the integer values.
- is_float(), is_double(), is_real() – Similarly, these functions check float variables. And, is_double() and is_real() are the alias functions of is_float().
- is_string() – This functions will return TRUE, if the given variable is of the type string.
- is_array(), is_object – checks for PHP array and object type of data, respectively.
There are more such functions like, is_callable(), is_null(), is_resource() and etc., for checking PHP variable for appropriate data type.
Importing/Exporting PHP Variables
- import_request_variables() – This function is deprecated as of PHP version 5.3.0. Its purpose is to import PHP $_REQUEST variables into global scope, where register_globals directive of php.ini file is disabled.
- var_export() – This function accepts an optional return type parameter, which will be FALSE by default, and exporting variable data to the browser. While setting the optional parameter with TRUE, then, this function will return array to be stored into a variable.
These functions are used to display output to the browser instead of returning something to be stored into a variable.
- print_r() – It will print array of data to the browser.
- var_dump() – It will print array of data with more information to the browser. For example, it will print data with its length, type information.
In PHP, var_export() is capable of both for displaying output to the browser and for storing it to a variable, on need basis.
Other PHP variable Functions
- get_defined_vars() – This function is used for getting an array of all defined variable of a PHP including global variables.
- empty() – This function will be used in PHP if/else statements, as like as, is_array(), is_numeric() and etc. This is for checking whether the given variable is empty or not.
- isset() – PHP isset() function is used to check whether a variable is defined or not, and no matter, if no values initialized with it.
- unset() – It will clear the value set with a variable.
- seralize()/unseralize() – seralize() converts value of a variable into storable data format, whereas, unseralize() is used to convert storable data format back to its original form.
This PHP code tutorial was published on August 27, 2013.
↑ Back to Top