How to write the variables in this simple loop? It's easy. I'm a newbie and don't know how to write.

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.