PHP compact() Function

What is compact() Function?

Explanation

The "compact()" function creates an array containing variables and their values.

Syntax:


compact(var1,var2...)

In the above syntax "var1" can be a string with the variable name, or an array of variables, "var2" is optional.

Example :


<?php
$Country= "United Sates"
$State= "Arkansas"
$Code= "561"
$address = compact("Country", "State", "Code");
print_r($address);
?>
Result :

Array
{
[Country] => United States
[State] => Arkansas
[Code] => 561
}

In the above example variables provided in the "compact" function are taken as keys to create an array.

PHP Topics


Ask Questions

Ask Question