PHP compact() 函數(shù)
PHP compact() 函數(shù)
實(shí)例
創(chuàng)建一個(gè)包含變量名和它們的值的數(shù)組:
<?php
$firstname = "Peter";
$lastname = "Griffin";
$age = "41";
$result = compact("firstname", "lastname", "age");
print_r($result);
?>
$firstname = "Peter";
$lastname = "Griffin";
$age = "41";
$result = compact("firstname", "lastname", "age");
print_r($result);
?>
運(yùn)行實(shí)例 ?
定義和用法
compact() 函數(shù)創(chuàng)建一個(gè)包含變量名和它們的值的數(shù)組。
注釋:任何沒有變量名與之對(duì)應(yīng)的字符串都被略過。
語法
compact(var1,var2...)
參數(shù) | 描述 |
---|---|
var1 | 必需。可以是帶有變量名的字符串,或者是一個(gè)變量數(shù)組。 |
var2,... | 可選??梢允菐в凶兞棵淖址?,或者是一個(gè)變量數(shù)組。允許多個(gè)參數(shù)。 |
Technical Details
返回值: | 返回帶有所有變量名和它們的值的數(shù)組。 |
---|---|
PHP 版本: | 4+ |
更多實(shí)例
實(shí)例 1
使用沒有對(duì)應(yīng)變量名的字符串,以及一個(gè)變量名數(shù)組:
<?php
$firstname = "Peter";
$lastname = "Griffin";
$age = "41";
$name = array("firstname", "lastname");
$result = compact($name, "location", "age");
print_r($result);
?>
$firstname = "Peter";
$lastname = "Griffin";
$age = "41";
$name = array("firstname", "lastname");
$result = compact($name, "location", "age");
print_r($result);
?>
運(yùn)行實(shí)例 ?
