Latest web development tutorials

PHP compact () function

PHP Array Reference Complete PHP Array Reference

Examples

Create an array of variable names and their values ​​comprising:

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

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

print_r($result);
?>

Running instance »

Definition and Usage

compact () function to create an array of variable names and their values ​​contained.

Note: No no corresponding variable name string are skipped.


grammar

compact( var1,var2... )

参数 描述
var1 必需。可以是带有变量名的字符串,或者是一个变量数组。
var2,... 可选。可以是带有变量名的字符串,或者是一个变量数组。允许多个参数。

Technical Details

return value: It returns an array with all the variables and their values.
PHP version: 4+


More examples

Example 1

Use no corresponding string variable name and a variable name array:

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

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

print_r($result);
?>

Running instance »


PHP Array Reference Complete PHP Array Reference