`compact()` Function in PHP 8.2, PHP 8.3 & PHP 8.4
The `compact()` function in PHP is used to create an associative array using variable names as keys and their corresponding values. It helps in dynamically constructing arrays from existing variables.
Syntax:
array compact(mixed $var_name, mixed ...$var_names)
Example:
<?php $name = "John"; $age = 30; $city = "New York"; $result = compact("name", "age", "city"); print_r($result); ?>
Output:
Array ( [name] => John [age] => 30 [city] => New York )