compact()

EspressoCat's avatar
Published Aug 1, 2023
Contribute to Docs

The compact() function generates an array from variables and their values.

Syntax

compact($variable1, ...$variableN)

The compact() function accepts the following parameters:

  • $variable1: Can be a string with the variable name, or an array of variables.
  • $variableN(optional): These are additional parameters, and each can be a string with the variable name or an array of variables. Multiple parameters are allowed.

The compact() function returns an array with all the variables added to it.

Note: Any string that does not match a variable name will be skipped.

Example

The following example demonstrates a basic implementation of compact().

<?php
// Create some variables
$name = 'John';
$age = 30;
$city = 'New York';
// Use compact() to create an array from variables
$person = compact('name', 'age', 'city');
// Output the resulting array
print_r($person);
?>

The example will result in the following output:

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

Codebyte Example

This example is runnable and uses the compact() function:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn PHP on Codecademy