compactCREATE an ARRAY containing variables and their values.
This function looks for a variable with that name in the current symbol table and adds it to the output ARRAY such that the variable name becomes the key and the contents of the variable become the value for that key.
This function, in other words, does the oposite of the function extract.
Before PHP 7.3.0, any strings that are not set will silently be skipped.
This function takes a variable number of parameters where 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; this function handles it recursively.
This function issues an E_NOTICE level error if a given string refers to an unset variable.
The Superglobal arrays may not be passed into this function.
<?php
arr compact ( mix $varname1 [, mix $varname2, ..., mix $varnameN ] )
where,
$varname1 = First defined variable name
$varname2 = Second defined variable name
. . . . . . . . .
$varnameN = Last defined variable name
?>
$varname1
The FIRST defined variable name.
$varname2
The SECOND defined variable name.
$varnameN
The LAST defined variable name.
EXERCISE
<?php
$K = 2;
$L = 8;
$M = 18;
$N = 32;
$O = 32;
$P = 18;
$Q = 8;
$layers = ["K", "L", "M", "N", "O", "P", "Q"];
$arrcomplay = compact($layers);
echo '<pre>';
print_r($arrcomplay);
echo '</pre>';
?>
RESULT
Array
(
[K] => 2
[L] => 8
[M] => 18
[N] => 32
[O] => 32
[P] => 18
[Q] => 8
)
EXERCISE
<?php
$RED = [ 255, 0, 0 ];
$GREEN = [ 0, 255, 0 ];
$BLUE = [ 0, 0, 255 ];
$mix02 = compact( "RED", "GREEN", "BLUE" );
echo '<pre>';
print_r($mix02);
echo '</pre>';
?>
RESULT
Array
(
[RED] => Array
(
[0] => 255
[1] => 0
[2] => 0
)
[GREEN] => Array
(
[0] => 0
[1] => 255
[2] => 0
)
[BLUE] => Array
(
[0] => 0
[1] => 0
[2] => 255
)
)
EXERCISE
<?php
$city = "San Francisco";
$state = "CA";
$event = "SIGGRAPH";
$location_vars = array("city", "state");
$result = compact("event", $location_vars);
var_dump($result);
?>
EXERCISE
<?php
/*
* Test basic functionality
*/
echo "Testing compact() : basic functionality.<br><br>";
$a=1;
$b=0.2;
$c=true;
$d=array("key"=>"val");
$e=NULL;
$f="string";
// simple array test
var_dump (compact(array("a", "b", "c", "d", "e", "f")));
echo '<br><br>';
// simple parameter test
var_dump (compact("a", "b", "c", "d", "e", "f"));
echo '<br><br>';
var_dump (compact(array("keyval"=>"a", "b"=>"b", "c"=>1)));
echo '<br><br>';
// cases which should not yield any output.
var_dump (compact(array(10, 0.3, true, array(20), NULL)));
echo '<br><br>';
var_dump (compact(10, 0.3, true, array(20), NULL));
echo '<br><br>';
var_dump (compact(array("g")));
?>
EXERCISE
<?php
var_dump("\0\0\0\0\0\0\0\0");
echo '<br><br>';
var_dump(0.0);
echo '<br><br>';
var_dump("\0\0\0\0\0\0\0\0");
echo '<br><br>';
var_dump(0.0);
?>
EXERCISE
<?php
$foo = null;
$bar = null;
var_dump(compact('foo', 'bar'));
echo '<br><br>';
var_dump(compact('bar', 'foo'));
?>
EXERCISE
<?php
echo "Testing compact() : usage variations -
variables outside of current scope.<br><br>";
$a = 'main.a';
$b = 'main.b';
function f() {
$b = 'f.b';
$c = 'f.c';
var_dump(compact('a','b','c'));
echo '<br><br>';
var_dump(compact(array('a','b','c')));
}
f();
?>