compact


php128 apg

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



  1 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
)


  2 EXERCISE   

<?php

$RED 
= [ 2550];
$GREEN = [ 0255];
$BLUE = [ 00255 ];

$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
)

)


  3 EXERCISE   

<?php

$city  
"San Francisco";
$state "CA";
$event "SIGGRAPH";

$location_vars = array("city""state");

$result compact("event"$location_vars);
var_dump($result);

?>

  4 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(100.3true, array(20), NULL)));
echo 
'<br><br>';
var_dump (compact(100.3true, array(20), NULL));
echo 
'<br><br>';
var_dump (compact(array("g")));

?>

  5 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);

?>

  6 EXERCISE   

<?php

$foo 
null;
$bar null;

var_dump(compact('foo''bar'));
echo 
'<br><br>';
var_dump(compact('bar''foo'));

?>

  7 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();

?>