How to use extract in PHP
In this article, I will explain how the extract() function can be used in PHP.
extract() function in PHP
Syntax
extract(array,extract_rules,prefix) |
Parameter
- array array is required parameter. it is the input array to use.
-
extract_rules extract_rules is optional parameter. The extract() function checks for invalid variable names and existing variable names. Possible values:
- EXTR_OVERWRITE - It's a default value. On collision, the existing variable is overwritten
- EXTR_SKIP - On collision, the existing variable is not overwritten
- EXTR_PREFIX_SAME - On collision, the variable name will be given a prefix
- EXTR_PREFIX_ALL - All variable names will be given a prefix
- EXTR_PREFIX_INVALID - Only invalid or numeric variable names will be given a prefix
- EXTR_IF_EXISTS - Only overwrite existing variables in the current symbol table, otherwise do nothing
- EXTR_PREFIX_IF_EXISTS - Only add prefix to variables if the same variable exists in the current symbol table
- EXTR_REFS - Extracts variables as references. The imported variables are still referencing the values of the array parameter
- prefix prefix is optional parameter. a specified prefix is required, if EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID or EXTR_PREFIX_IF_EXISTS are used in the extract_rules parameter.
Example
The following example show to how the extract() function can be used in PHP.
<html>
<body>
<h3 style="color: seagreen;">extract() function example in PHP</h3>
<?php
$size = "Large";
$array1 = array("Fname" => "Nitin",
"Lname" => "Bhardwaj",
"Age" => "22");
extract($array1, EXTR_PREFIX_SAME, "size");
echo "Name: ".$Fname."<br/>","Lname: ".$Lname."<br/>","Age: ".$Age."<br/>", "Size: ".$size;
?>
</body>
</html>
|
Output
You may also want to read these related articles here
Ask Your Question
Got a programming related question? You may want to post your question here