PHP - extract
Trieda
Metóda - extract
(PHP 4, PHP 5, PHP 7)
It extracts variables from an array into the current scope. It uses array keys as variable names, the function checks if variable names are valid and also if the variable doesn't already exist.
It's a potentially dangerous function and we should consider what data can get into the extracted array and if a user can access the array.
Procedurálne
- function extract (array &$array, int $flags = EXTR_OVERWRITE, string $prefix = NULL) : int
Parametre
| Názov | Dátový typ | Predvolená hodnota | Popis |
|---|---|---|---|
| &$array | array | The input array. | |
| $flags | int | EXTR_OVERWRITE | Specifies how collisions and invalid values are treated.
|
| $prefix | string | NULL | The third optional parameter is a prefix of the variable name. It's required with following flags: EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID or EXTR_PREFIX_IF_EXISTS. The underscore character("_") is automatically added after the prefix. |
Mávratovej hodnoty
Vracia: int
A number of successfully extracted variables.
Príklady
<?php
$array = ['a' => 1, 'b' => '2', 'c' => 3.0];
extract($array);
echo $a;
The extract() function can be used very well when creating
templates with own PHP structures.
