PHP - Function compact()


Advertisements


Syntax

compact($var1, $var2...);

Definition and Usage

This function takes a variable number of parameters. Each parameter can be either a string containing the name of the variable, or an array of variable names. The array can contain other arrays of variable names inside it; compact() handles it recursively.

Parameters

Sr.No Parameter & Description
1

var1

Required. Can be a string with the variable name, or an array of variables.

2

var2

Optional. Can be a string with the variable name, or an array of variables.

Return Value

It returns the output array with all the variables added to it.

Example

Try out following example −

<?php
   $city  = "San Francisco";
   $state = "CA";
   $event = "SIGGRAPH";
   
   $result = compact("city", "state", "event");
   print_r($result);
?> 

This will produce the following result −

Array ( [city] => San Francisco [state] => CA [event] => SIGGRAPH )

php_function_reference.htm