Did you know that PHP can extract values ​​from arrays as variables? extract()

avatar

If you already knew this you are an incredible developer, for me it is a new world. This programming language has very good functions that I don't know about, but it's easy to understand what they do.
Thanks to a video on youtube I found that you can do crazy things like create a variable from data inside an array with php.

diaphp.jpg
I was very surprised because one uses the variables to store data, but this is another world; It provides a greater advantage since it does not require filtering and verifying the data as it would have to be done with "javascript".



0
0
0.000
4 comments
avatar

The early versions of PHP automatically extracted $_POST, $_GET and $_COOKIE arrays as variables.

This made programming with PHP super easy as one could define variables in a form and use them in a script.

They deprecated this feature because PHP programmers were lazy about initializing variables. Specifically, there was a code base that did something like:


if ($user_pwd = $stored_pwd) {
  $hasAccess = 1;
}

if ($has_access == 1) {
  // hand user keys to kingdom
}

If a hacker called the code with ?has_access=1 in the URL, then they could get the keys to the kingdom.

!WINE

0
0
0.000
avatar

so will you recomend me use other method and not "extrac()" to get data from array as variables?

0
0
0.000
avatar

I think PHP extract is a wonderful tool.

Personally I stopped using it. I like to explicitly initialize all the variables in a program.

I also don't like making copies of data. Anymore, I just draw data directly from the array.

Whenever I do use extract, I use it at the beginning of the program to assure that extract does not override any variables that I declared.

0
0
0.000