I was looking for a shortcut to easy up my variable declaration in PHP coding, the extract and compact functions are pretty good for that. But don’t think this will help you in all the cases, I strongly recommend that don’t use this functions directly unless your variable/values are sanitized. Because when you are using this directly in to a MySQL query statement with values from the input request variable will increase the security risks.
Okey, lets assume that your data are sanitized well, then look into the following.
extract — Extract function import the variable from an array to the current symbol table.
ie. if you have an associative array like $a = array(‘user_name’ => ‘midhun devasia’, ‘email_id’ => ‘mymail@mydomain.com’), and wanted to use this as variable name, we usually try this method $user_name = $a['user_name']; instead of that we will get all variable into our current symbol table by using extract function.
$a = array('user_name' => 'midhun devasia', 'email_id' => 'myemail@mydoamin.com');
extract($a);
echo "Hello ", $user_name, "Your email id is ", $email_id;
The first param is the array, and second param is optional , there you can pass to overwrite the variable values, add prefix etc options. see the PHP Manuel for more info.
compact — Create array containing variables and their values.
If you want to pass some variable values into an array as its key and values.
$user_name = "Midhun Devasia";
$email_id = "myemail@domain.com";
$result = compact("user_name", "email_id");
/*
echo $result['user_name'] is Midhun Devasia
*/

Thanks friend. This is really useful piece of info. I think extract function very useful in several scenarios.
Good post! Also, if you want to get values in an associative array to separate variables with array keys as names, you can use list() construct.
http://php.net/manual/en/function.list.php
hm, that is also a good solution, but I think the extract function is more powerful than list(), even extract is less known function in php.