Home >Backend Development >PHP Tutorial >Conversion between PHP variables and arrays (extract and compact)_PHP tutorial
In PHP, we can use the extract or compact function to convert arrays and variables. Let me share two examples with you using these two functions.
compact multiple variables to array
The code is as follows
|
Copy code
|
||||||||||||
$name='phpff'; $email='phpff@phpff.com'; $info=compact('name','email');//Pass variable name Print_r($info); /* Array ( [name] => phpff [email] => phpff@phpff.com ) */ ?>
extract array to multiple variables
$capitalcities['England'] = 'London'; $capitalcities['Scotland'] = 'Edinburgh'; $capitalcities['Wales'] = 'Cardiff'; Extract($capitalcities);//Convert into three variables England, Scotland, Wales Print $Wales;//Cardiff ?> |