How to write the variables in this simple loop? It's easy. I don't know how to write for ($ k1; $ k & lt; 4; $ k ++) {& nbsp; $ blName _. $ k hello. $ k; & nbsp;} indicates that I want to increment the value of $ blName_1, followed by the number 1: $ blName_1, $ blName_2, $ blName_3, replace it with a variable loop. how to write the variables in this simple loop? It's easy. I'm a newbie and don't know how to write it.
For ($ k = 1; $ k <4; $ k ++ ){
$ BlName _. $ k = ''. $ k;
}
// I want to increment the value of $ blName_1, followed by the number 1: $ blName_1, $ blName_2, $ blName_3, and replace it with a variable loop, how should I write it?
------ Solution --------------------
For ($ k = 1; $ k <4; $ k ++ ){
$ Varname = 'blname _ '. $ k;
$ {$ Varname} = ''. $ k;
}
// $ Varname = $ blName_1 because $ varname = 'blname _ 1;
This is generally not the case. in this case, an array should be used.
PHP code
Http://www.php.net/manual/zh/language.variables.variable.php
------ Solution --------------------
PHP code
For ($ k = 1; $ k <4; $ k ++) {$ {"blName _ {$ k}"} = 'Hello '. $ k ;}
------ Solution --------------------
PHP code
$ Arr = array (); for ($ k = 1; $ k <4; $ k ++) {array_push ($ arr, ''. $ k);} list ($ blName_1, $ blName_2, $ blName_3) = $ arr;
------ Solution --------------------
$ Var = array ();
For ($ k = 1; $ k <4; $ k ++ ){
$ Var [] = ''. $ k;
}
Print_r ($ var );
// Output
Array (
[0] => 'Hello 1 ';
[1] => 'Hello 2 ';
[2] => 'Hello 3 ';
)
// Set the value to $ var [1] and the value to 'Hello 2 '.
Take a look at the manual for good http://www.php.net/manual/zh/language.types.array.php
------ Solution --------------------
$ {"BlName $ k"} = 'Hello '. $ k;
Php recommended syntax
$ Varname = 'blname ';
For ($ k = 1; $ k <4; $ k ++ ){
$ BlName = $ varname. $ k;
$ BlName = ''. $ k;
}
What does it mean to include variables?
$ {$ Varname}
And $ {"blName _ {$ k }"}
Curly brackets refer to the enclosed parts as a whole.