PHP Compact creates an array from a variable
The compact function creates an array of one or more variables, and the array includes the variable names and their values.
The function behaves exactly the opposite of extract ().
Basic syntax
Array compact (mixed $varname [, mixed $ ...])
Creates an array that contains the variables and their values.
For each parameter, the compact () looks for the variable name in the current symbol table and adds it to the output array, and the variable name becomes the key and the contents of the variable becomes the value of the key. Simply put, it does the exact opposite of extract (). Returns the array after which all the variables are added.
Any string that does not have a variable name is skipped.
Parameter introduction:
Parameters |
Description |
VarName |
Necessary. Can be a string with a variable name, or an array of variables. |
... |
Optional. Can be a string with a variable name, or an array of variables. Multiple parameters are allowed. |
return value
Returns an array with all the variable names and their values.
Attention:
- Any string that does not have a variable name is skipped.
- If an array exists in the argument, the value of the variable in the array is also fetched.
- Because a variable variable may not be used inside a function for a super global array of PHP, you cannot pass a super global array into the compact ().
Instance:
<?php
$city = "San Francisco";
$state = "CA";
$event = "SIGGRAPH";
$location _vars = Array (
"City",
"state"
);
$result = Compact ("event", "Nothing_here", "Location_vars");
Print_r ($result);
echo "<br/><br/>";
$result = Compact ("event", "Nothing_here", $location _vars);
Print_r ($result);
Run Result:
Array ([Event] => SIGGRAPH [location_vars] => Array ([0] => City [1] => State)
Array ([Event] => SIGGRAPH [city] => San Francisco [State] => CA)
Thank you for reading, I hope to help you, thank you for your support for this site!