php

How to Use extract() Function in PHP

The extract() function in PHP allows you to easily extract content and variables to different locales, saving you time and effort. In this article, we will explore the benefits of using the extract() function and provide some examples to help you use it effectively in your PHP projects.

What is the extract() Function

The extract() is a built-in PHP function that allows you to extract array elements and variables into local variables with the same name as the array key or variable name. This means that instead of typing names or variable names each time, you can access items or variables using just their names. However, it is important to use extract() carefully, as it can cause random crashes and security issues if misused.

Syntax for extract() Function

The syntax for the extract() function in PHP is as follows:

extract(array, extract_rules, prefix)

The $extract_rules parameter is a second option that specifies how the variable list will be resolved. This parameter allows you to set various flags to control the behavior of the extract() function.

How to Use the extract() Function in PHP

Use the following steps to learn the use of the extract() function in PHP.

Step 1: First, create an array containing the variables to be extracted.

Step 2: Next, call the extract() function and pass the array as the first parameter.

Step 3: You can now access the array contents as local variables.

Step 4: Optionally by passing the $extract_rules parameter, if there is a name conflict, you can specify what to do.

Step 5: Finally, access the variable using the echo statement with the predefined name.

Here is a simple code that follows the above-given steps:

<?php

$data = array(

'name' => 'Awais',

'age' => 27,

'country' => 'US'

);

extract($data, EXTR_PREFIX_ALL, 'prefix');

echo $prefix_name . " \n";

echo $prefix_age . " \n";

echo $prefix_country . " \n";

echo "My name is " . $prefix_name . ", and I am " . $prefix_age . " years old from " . $prefix_country . ".";

?>

Note: In the above example prefix can be any name.

Here is another simple example that uses the extract() function to use the same array but includes only the data argument.

<?php

// Create an array of variables

$data = array(

'name' => 'Awais',

'age' => 27,

'country' => 'US'

);

// Extract the variables into the table

extract($data);

// Access the variables as local variables

echo "My name is " . $name . ", and I am " . $age . " years old from " . $country . ".";

?>

Why Use the extract() Function with EXTR_PREFIX_ALL and Without EXTR_PREFIX_ALL

The EXTR_PREFIX_ALL flag is used in the extract() function to avoid variable name conflicts.

When using the EXTR_PREFIX_ALL flag, all variables in the array will be prefixed with “prefix_”, as indicated by the second argument to extract(). This means you have to use $prefix_name instead to access the $name variable.

On the other hand, if the EXTR_PREFIX_ALL flag is not used, changes in the array are stripped without a prefix. This means you can directly access the variable from the $name without using the name first.

So, in the code above, if you remove the EXTR_PREFIX_ALL flag, you need to change the variable names in the echo statement from $prefix_name, $prefix_age, and $prefix_country to $name, $age, and $country.

Conclusion

The extract() function in PHP allows developers to easily access array elements and make changes without writing long lines of code. However, you must use this function with care as you may experience problems if used incorrectly. Using the EXTR_PREFIX_ALL flag, developers can avoid variable name conflicts and easily manage changes.

About the author

Awais Khan

I'm an Engineer and an academic researcher by profession. My interest for Raspberry Pi, embedded systems and blogging has brought me here to share my knowledge with others.