Copyright © tutorialspoint.com

PHP Function compact()

previous
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.

Paramters

ParameterDescription
var1Required. Can be a string with the variable name, or an array of variables.
var2Optional. 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 following result:

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

Copyright © tutorialspoint.com