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

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

WBOY
WBOYOriginal
2016-07-13 09:58:08829browse

How PHP uses the key value of the post array to create a variable with the same name and assign a value

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

1

2

3

4

5

6

7

8

9

10

11

$expected=array('username','age','city','street');

foreach($expected as $key){

if(!empty($_POST[$key])){

${key}=$_POST[$key];

}

else{

${key}=NULL;

}

}

?>

1 2

3

4

5

7 8 9 10 11
$expected=array('username','age','city','street');
<🎜>foreach($expected as $key){<🎜> <🎜>if(!empty($_POST[$key])){<🎜> <🎜>${key}=$_POST[$key];<🎜> <🎜>}<🎜> <🎜>else{<🎜> <🎜>${key}=NULL;<🎜> <🎜>}<🎜> <🎜>}<🎜> <🎜>?>
I hope this article will be helpful to everyone’s PHP programming design. http://www.bkjia.com/PHPjc/979232.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/979232.htmlTechArticlephp uses the key value of the post array to create a variable with the same name and assign a value. This example describes how php uses the key value of the post array. A method to create a variable with the same name and assign a value. Share it with everyone for your reference...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn