Home >Backend Development >PHP Tutorial >How to use the key value of the post array to create a variable with the same name and assign a value in PHP_PHP Tutorial
This article describes the example of how PHP uses the key value of the post array to create a variable with the same name and assign a value. Share it with everyone for your reference. The details are as follows:
This code can automatically create a variable with the same name based on the key value of the post array. This function is very convenient to use without having to declare the variable in advance
3 45 |
$expected=array('username','age','city','street');
<🎜>foreach($expected as $key){<🎜>
<🎜>if(!empty($_POST[$key])){<🎜>
<🎜>${key}=$_POST[$key];<🎜>
<🎜>}<🎜>
<🎜>else{<🎜>
<🎜>${key}=NULL;<🎜>
<🎜>}<🎜>
<🎜>}<🎜>
<🎜>?>
|