PHP compact() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, compact() creates an array from variables, using the variable names as keys.

You pass the names of the variables as strings (without the $ sign). It is the opposite of extract(), and it is handy for passing data to a template or returning several values at once.

Syntax

Call compact() with the values you need. Start with this line.

PHP
$data = compact("name", "age");
CallWhat it does
compact("name", "age")Builds ["name" => ..., "age" => ...] from two variables.
compact(["a", "b"])Accepts an array of variable names too.
compact("a", ["b", "c"])Mixes strings and arrays of names.

compact() makes a snapshot of the variables at that moment. Changing the variables later does not change the array. A variable that does not exist produces a warning, so define every variable first.

Library declarations
PHP
compact(array|string $var_name, array|string ...$var_names): array

Parameters

ParameterDescription
var_nameRequired. A variable name as a string, or an array of variable names. Do not include the $ sign.
var_namesOptional. Any number of extra names, or arrays of names.

Return Value

Return ValueDescription
arrayAn associative array where each key is a variable name and each value is that variable's value.

PHP Examples

Example 1 — Build an Array from Variables

Collect two variables into one array.

PHP
<?php
$name = "Ann";
$age = 30;

$data = compact("name", "age");

echo json_encode($data), PHP_EOL;

How It Works

  1. Create two ordinary variables, $name and $age.
  2. compact() looks up each variable by its name.
  3. It builds an array with the names as keys and the values as values.
  4. json_encode() prints the resulting array.

Example 2 — Arrays of Names and Snapshots

You can mix names and arrays of names, and the result is a copy.

PHP
<?php
$a = 1;
$b = 2;
$c = 3;
echo json_encode(compact("a", ["b", "c"])), PHP_EOL;

$x = 5;
$copy = compact("x");
$x = 10;
echo $copy["x"], PHP_EOL;

How It Works

  1. compact() accepts a name string followed by an array of names.
  2. All three variables end up in a single flat array.
  3. $copy is built when $x is 5.
  4. Changing $x to 10 afterward does not change $copy, so it still prints 5.

Example 3 — Return Several Values

Use compact() inside a function to return a bundle of values.

PHP
<?php
function buildUser() {
    $name = "Ann";
    $role = "admin";
    return compact("name", "role");
}

$user = buildUser();

echo "Name: ", $user["name"], PHP_EOL;
echo "Role: ", $user["role"], PHP_EOL;

How It Works

  1. The function creates two local variables.
  2. compact() packs them into one associative array and returns it.
  3. The caller receives the array in $user.
  4. Each value is read by its key and printed.

Frequently Asked Questions

It creates an associative array from variables, using the variable names as keys and their values as values.
extract(). It turns the keys of an array into variables.
No. Pass only the name, such as compact("name").
PHP skips it and shows a warning in PHP 8. Always define the variable before calling compact().
Yes. You can pass names as separate strings, as arrays of strings, or a mix of both.

Next: count()

count() returns the number of items in an array.

count() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful