PHP compact()Functions

PHP Array ReferenceComplete PHP Array Reference

Examples

Create an array containing variable names and their values:

<?php
$firstname = "Peter";
$lastname = "Griffin";
$age = "41";

$result = compact("firstname", "lastname", "age");

print_r($result);
?>

Run example »

Definition and Usage

The compact() function creates an array containing variable names and their values.

Note:Any string that does not correspond to a variable name is skipped.


Syntax

compact(var1,var2...)

Parameter Description
var1 Required. Can be a string containing variable names, or an array of variables.
var2,... Optional. Can be a string containing variable names, or an array of variables. Multiple parameters are allowed.

Technical Details

Return value: Returns an array containing all the variable names and their values.
PHP Version: 4+


More Examples

Example 1

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);
?>

Run example »


PHP Array ReferenceComplete PHP Array Reference Other Extensions