compact

FunctionParams and return types changed in PHP 8.0

Create array containing variables and their values.

PHP 5
PHP 7
PHP 8.0
Improved
PHP 8.1
PHP 8.2-8.3
PHP 8.4
PHP 8.5
PHP 8.6

compact Function synopsis

compact($var_name, ...$var_names): array

Parameters

$var_name

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.

$var_names

Variadic, Optional

Return value

Typearray

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

Changes to the compact Function

PHP 8.0

  • Return type added: array
  • Optional parameter added - #2: $var_names
- compact(...$var_names)
+ compact($var_name, ...$var_names): array

compact Function Availability

PHP VersionAvailability
PHP 8.6Upcoming Release Yes
PHP 8.5Supported (Latest) Yes
PHP 8.4Supported Yes
PHP 8.3Security-Fixes Only Yes
PHP 8.2Security-Fixes Only Yes
PHP 8.1Unsupported Yes
PHP 8.0Unsupported Yes
PHP 7.4Unsupported Yes
PHP 7.3Unsupported Yes
PHP 7.2Unsupported Yes
PHP 7.1Unsupported Yes
PHP 7.0Unsupported Yes
PHP 5.6Unsupported Yes
PHP 5.5Unsupported Yes
PHP 5.4Unsupported Yes
PHP 5.3Unsupported Yes