Create an array containing variable names and their values:
<?php$firstname = "Peter";$lastname = "Griffin";$age = "41";$result = compact("firstname", "lastname", "age");print_r($result);?>The compact() function creates an array containing variable names and their values.
Note: Any string that does not have a corresponding variable name is ignored.
compact( var1,var2... )
parameter | describe |
---|---|
var1 | Required. Can be a string with a variable name, or an array of variables. |
var2,... | Optional. Can be a string with a variable name, or an array of variables. Multiple parameters are allowed. |
Return value: | Returns an array with all variable names and their values. |
---|---|
PHP version: | 4+ |
Use a string with no corresponding variable name, and an array of variable names:
<?php$firstname = "Peter";$lastname = "Griffin";$age = "41";$name = array("firstname", "lastname");$result = compact($name, "location", "age");print_r($result);?>