Automatically creates variables with the same name as the key in the POST array

This snippet is very helpful for every POST processing. All you need is an array with expected keys in the POST array. This snippet automatically creates variables with the same name as the key in the POST array. If the key is not found in the POST array the variable is set to NULL. Basically you dont need to write:


This snippet will do this boring part of every PHP code with POST handling so you can fully focus on a validation of the input, because that is much more important.

foreach($expected as $key){
  • Hi,

    just have a look at this:

    But I think it’s an unclean technique, it mimics the behaviour of register_globals either way… just as the php-manual states.


  • Bare with me guys, sorry. Too late and read on the phone… This snippet is absolutely ok!

    Your first define the key’s you want to use from $_POST and dont get everything from the super-global-array to the local scope…

  • Pako!

    I also know another way (3 ways to be exactly):

    extract($_POST) / extract($_GET) / extract($_REQUEST)

    meowww… (:

  • Hey dude! You need to pass a var name inside the braces. If you do not do it, you’re passing a constant… so where is ${key} use ${$key} or simply $$key. 😉