Is it preferred to assign POST variable to an actual variable?

后端 未结 5 2252
半阙折子戏
半阙折子戏 2021-02-20 18:29

I\'ve just completed my registration form for my website and for the action page where all the SQL takes place I\'ve just skipped assigning the POST variable to actual ones, lik

5条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-20 19:12

    I see no advantage or disadvantage. Once you start modifying the values, you should put them into their own variable, but if you're just reading them, you can leave them where they are. Only two points:

    • If it makes your source code more readable to use short variables names instead of $_POST[...], that's a good reason to put the values into their own variables.
    • Don't necessarily take values out one by one, but just assign the array contents into another array:

      $values = $_POST;
      
      // not:
      
      $foo = $_POST['foo'];
      $bar = $_POST['bar'];
      ...
      

提交回复
热议问题