Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

compact()

compact() is a native PHP function, which creates an array containing variables and their values, from a list of variable names.

It is the opposite of extract().

<?php

    $a = '1';
    $b = '2';
    
    $array = compact('a', 'b');
    // ['a' => 1, 'b' => 2];

?>

Documentation

See Also