• PHP Video Tutorials

PHP - Function compact()



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.

Parameters

Sr.No Parameter & Description
1

var1(Required)

It can be a string with the variable name, or an array of variables.

2

var2(Optional)

It 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 = "New Year";
   $result = compact("city", "state", "event");
   print_r($result);
?> 

This will produce the following result −

Array ( [city] => San Francisco [state] => CA [event] => New Year )
php_function_reference.htm
Advertisements