Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

compact()

Gather named variables from the current scope into an associative array.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

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

Gather named variables from the current scope into an associative array. Supply names without the dollar sign, separately or in nested arrays. A defined null value is retained. In PHP 8 an undefined name emits E_WARNING and contributes no key.

Signature

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

Example output

{"units":3,"memo":null}

Examples

<?php
$units = 3; $memo = null;
echo json_encode(compact(["units", ["memo"]]));

Result

{"units":3,"memo":null}

Sources