W3docs

PHP Array Compact Function

Learn how PHP's compact() function builds an associative array from existing variables, with syntax, examples, and common gotchas explained.

What is the PHP compact() function?

compact() is a built-in PHP function that builds an associative array from existing variables. You pass it the names of variables (as strings), and it returns an array whose keys are those names and whose values are the current values of the variables. It is the inverse of extract(), which turns an array back into individual variables.

This page covers the syntax, how compact() resolves variable names, passing names as an array, what happens with null and undefined variables, and a common real-world use case.

Syntax

compact(string|array ...$var_names): array

Each argument is either a string holding a variable name, or an array of such strings. compact() returns the resulting associative array.

How does it work?

compact() searches the current scope for each name you give it. If a variable with that name exists, it is added to the result (the name becomes the key, the variable's value becomes the value) — even when the value is null. If no such variable exists, that name is skipped, and PHP 8.0+ emits a warning ("Undefined variable").

Here is the simplest case:

Basic compact() example

php— editable, runs on the server

We create three variables and pass their names to compact(). The resulting array looks like this:

Array (
  [name] => John
  [age] => 30
  [city] => New York
)

Note that you pass the names without the leading $"name", not $name.

Why use compact()?

  • Less repetition: Instead of writing ['name' => $name, 'age' => $age, 'city' => $city], you write compact('name', 'age', 'city'). The key and the variable name stay in sync automatically.
  • Readability: When the array key and the variable already share a name, compact() removes the visual noise of duplicating each one.
  • Fewer typos: Hand-writing key-value pairs makes it easy to mistype a key. With compact() the key is derived from the variable name.

Examples

Including a variable with a null value

php— editable, runs on the server

Here $state is defined but holds null. Because the variable exists, it is still included — its value in the array is null (which print_r shows as an empty value):

Array (
  [name] => John
  [age] => 30
  [city] => New York
  [state] => 
)

A name that has no matching variable in scope is a different case: it is skipped entirely (and PHP 8.0+ raises a warning). So compact('name', 'missing') returns only ['name' => 'John'].

Passing the names as an array

Any argument can itself be an array of names, which compact() flattens. This is handy when the list of fields is built dynamically:

<?php

  $name = "John";
  $age  = 30;
  $city = "New York";

  $fields = ["name", "age"];
  $myArray = compact("city", $fields);

  print_r($myArray);

?>
Array (
  [city] => New York
  [name] => John
  [age] => 30
)

Real-world use: passing data to a template

compact() shines when you want to hand a view or template a bundle of named values without building the array by hand:

<?php

  function renderProfile() {
    $username = "jdoe";
    $role     = "admin";
    $isActive = true;

    // Pass all three as a tidy associative array
    return compact("username", "role", "isActive");
  }

  print_r(renderProfile());

?>
Array (
  [username] => jdoe
  [role] => admin
  [isActive] => 1
)

Tips and gotchas

  • Pass variable names without the $ sign: compact("name"), not compact($name).
  • compact() reads the current scope only — inside a function it sees that function's local variables, not globals.
  • It includes variables whose value is null, but skips names with no matching variable (warning in PHP 8.0+). Use isset() or define the variable first if you need it present.
  • To go the other direction — turning an array into variables — use extract(). To inspect what variables exist in scope, see get_defined_vars().

Practice

Practice
What is the role of the 'compact()' function in PHP?
What is the role of the 'compact()' function in PHP?
Was this page helpful?