extract() function in PHP


The extract() function imports variables into the current symbol table from an array. It returns the number of variables successfully extracted.

Syntax

extract(arr, rules, prefix)

Parameters

  • arr − The specified array

  • rules − Specifies how to deal with invalid variable name. The following are the possible values −

    • EXTR_OVERWRITE − Default. 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 − needed only for the following: EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALIDor EXTR_PREFIX_IF_EXISTS.

Return

The extract() function returns the number of variables successfully extracted.

Example

The following is an example −

 Live Demo

<?php
$prod = array("AM"=>"AMIT", "TM"=>"Tom");
extract($prod);
echo"\$AM is $AM
\$TM is $TM"; ?>

Output

$AM is AMIT
$TM is Tom

Updated on: 24-Jun-2020

76 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements