Friday, October 30, 2020

PHP extract() Function

PHP extract() Function


Hello dear readers! welcome back to another section of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP extract() Function.

PHP extract() function is used for importing variables from an array into the current symbol table.

It takes an associative array array and treats keys as variable names and values as variable values. For each key/value pair it will create a variable in the present symbol table, subject to extract_type and prefix parameters.

Syntax

Following below is the syntax to use this function -

extract($array, $extract_type, $prefix)


Parameter Details

Sr.NoParameter & Description
1

array(Required)

It specifies an array

2

extract_type(Optional)

. The extract() function checks for invalid variable names and collisions with existing variable names. This parameter specifies how invalid and colliding names are treated.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

3

prefix(Optional)

If EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID or EXTR_PREFIX_IF_EXISTS are used in the extract_rules parameter, a specified prefix is required.

This parameter specifies the prefix. The prefix is automatically separated from the array key by an underscore character.


Return Value

This built-in function returns the number of variables successfully imported into the symbol table.

Example

Try out the below example -

<?php
   $size = "large";
   $input = array("color" => "blue", "size"  => "medium", "shape" => "sphere");
   extract($input, EXTR_PREFIX_SAME, "bbcx");
   echo "$color, $size, $shape, $bbcx_size";
?> 

Output

When the above code is executed, it will produce the following result -

blue, large, sphere, medium


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the PHP in_array() Function.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.