Definition
The compact() function creates an array from variables and their values.
Syntax
compact(varname1, varname2, ...)
Parameters
Parameter | Description |
---|---|
varname1 |
Required. Can be a string representing variable name, or an array of variables. |
varname2... |
Optional. Can be a string representing variable name, or an array of variables. Multiple parameters are allowed. |
Example
<?php
$name = "Mark";
$age = 22;
$country = "UK";
// Example 1
$result = compact("name", "age");
print_r($result);
echo "<br>";
// Example 2
$person = array("name", "age");
$result = compact($person, "country");
print_r($result);
echo "<br>";