PHP compact Function
last modified March 13, 2025
The PHP compact
function creates an array containing variables
and their values. It's useful for quickly bundling variables into an array.
Basic Definition
The compact
function takes variable names as arguments and
creates an array where keys are variable names and values are variable values.
Syntax: compact(string|array $var_name, ...): array
. It accepts
one or more strings or an array of strings representing variable names.
Basic compact Example
This demonstrates creating an array from three simple variables using compact.
<?php $name = "John"; $age = 30; $city = "New York"; $person = compact('name', 'age', 'city'); print_r($person);
The function creates an associative array from the variables passed as arguments.
Using compact with an Array of Names
Instead of multiple arguments, you can pass an array of variable names.
<?php $firstName = "Alice"; $lastName = "Smith"; $email = "alice@example.com"; $vars = ['firstName', 'lastName', 'email']; $userData = compact($vars); print_r($userData);
This produces the same result as passing names individually. The array approach is useful when variable names are stored dynamically.
Nested Variables with compact
compact can work with nested variable names when using array syntax.
<?php $user = [ 'name' => 'Bob', 'profile' => [ 'age' => 25, 'job' => 'Developer' ] ]; extract($user); $result = compact('name', ['profile' => ['age', 'job']]); print_r($result);
This shows how to compact nested variables. Note that you need to extract the array first to make variables available in the current symbol table.
compact with Undefined Variables
compact silently ignores undefined variables without throwing errors.
<?php $definedVar = "I exist"; $result = compact('definedVar', 'undefinedVar'); print_r($result); // Only shows definedVar
The output only contains the defined variable. This behavior can be useful when you're not sure which variables exist in the current scope.
Combining compact with extract
compact and extract can be used together for variable manipulation.
<?php $original = [ 'title' => 'PHP Guide', 'author' => 'Jane Doe', 'pages' => 350 ]; extract($original); $modified = compact('title', 'author', 'pages'); $modified['edition'] = 2; print_r($modified);
This extracts variables from an array, then compacts them back with an additional field. It demonstrates round-trip variable conversion.
Best Practices
- Variable Scope: compact only works with variables in current scope.
- Dynamic Names: Use array syntax for dynamic variable names.
- Error Handling: Check for undefined variables if needed.
- Performance: Avoid compact in tight loops with many variables.
Source
This tutorial covered the PHP compact
function with practical
examples showing its usage for creating arrays from variables.
Author
List all PHP Array Functions.