Menu Home

compact

osbo.com | PHP | Functions | compact

Create array containing variables and their values

Syntax

compact ( mixed $varname1 [, mixed $... ] ) : array

Parameters

varname1

compact() takes a variable number of parameters. Each parameter can be either a string containing the name of the variable, or an array of variable names. The array can contain other arrays of variable names inside it; compact() handles it recursively.

Return

Returns the output array with all the variables added to it.

Examples

<?

$memorial = "Mount Rushmore National Memorial";
$city  = "Keystone";
$state = "South Dakota";
$location = array("city", "state");

$return = compact("memorial", $location);
print_r($return);

?>
Array
(
    [memorial] => Mount Rushmore National Memorial
    [city] => Keystone
    [state] => South Dakota
)

PHP