Home  >  Article  >  Backend Development  >  How to use compact function

How to use compact function

藏色散人
藏色散人Original
2019-02-12 09:41:442521browse

PHP compact() function is used to create an array, including variable names and their values.

How to use compact function

php compact() function syntax

Function: Create an array containing variable names and their values

Syntax:

compact(var1,var2...)

Parameters:

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.

Description: Create an array containing variable names and their values. Any string that does not have a corresponding variable name is ignored.

php compact() function example 1

<?php
$firstname = "西门";
$lastname = "灭绝";
$age = "25";
$result = compact("firstname", "lastname", "age");
print_r($result);
?>

Output:

Array ( [firstname] => 西门 [lastname] => 灭绝 [age] => 25 )

php compact() function example 2

<?php
$name = "无忌";
$job = "讲师";
$age = "20";
$result = compact("name", "job", "age");
print_r($result);
?>

Output:

Array ( [name] => 无忌 [job] => 讲师 [age] => 20 )

This article is an introduction to the PHP compact function. I hope it will be helpful to friends in need!

The above is the detailed content of How to use compact function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn