PHP compact() 函数

PHP 教程 · 2019-01-22 17:56:27

实例

创建一个包含变量名和它们的值的数组:

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

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

print_r($result);
?>

定义和用法

compact() 函数创建一个包含变量名和它们的值的数组。

注释:任何没有变量名与之对应的字符串都被略过。

语法

compact(var1,var2...)

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

Technical Details

返回值: 返回带有所有变量名和它们的值的数组。
PHP 版本: 4+

更多实例

实例 1

使用没有对应变量名的字符串,以及一个变量名数组:

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

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

print_r($result);
?>

点击查看所有 PHP 教程 文章: https://www.codercto.com/courses/l/5.html

查看所有标签

Building Social Web Applications

Building Social Web Applications

Gavin Bell / O'Reilly Media / 2009-10-1 / USD 34.99

Building a social web application that attracts and retains regular visitors, and gets them to interact, isn't easy to do. This book walks you through the tough questions you'll face if you're to crea......一起来看看 《Building Social Web Applications》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具