阅读Google Developers PHP性能提示我看到不建议制作变量的额外副本.
而不是这个:
$description = strip_tags($_POST['description']);
echo $description;
Run Code Online (Sandbox Code Playgroud)
它推荐这个:
echo strip_tags($_POST['description']);
Run Code Online (Sandbox Code Playgroud)
原因是可能不必要的内存消耗.
但是做了一些搜索,我看到一些反驳说PHP实现了"copy-on-write"内存管理.这基本上意味着我们可以为我们喜欢的变量分配一个值,而不必担心实际复制的数据.
所以,我想知道,如果在更复杂的情况,其中,例如$_POST或$_GET变量将在代码中的很多地方都可以使用,无论是更好的做法是使用或不使用额外的变量,考虑到这些标准:
1)安全
2)维护/可读性
3)表现
编辑1
我将使用下面的示例来更好地说明问题.
这种代码更好(考虑上面的标准):
$user = anti_injection($_POST['user']);
$pass = anti_injection($_POST['pass']);
// Continue the code using $user and $pass
Run Code Online (Sandbox Code Playgroud)
或这个?
$_POST['user'] = anti_injection($_POST['user']);
$_POST['pass'] = anti_injection($_POST['pass']);
// Continue the code using $_POST['user'] and $_POST['pass']
Run Code Online (Sandbox Code Playgroud)