This simple loop, the inside of the variable exactly how to write it? It's simple, I'm a novice, I don't know how to write

Source: Internet
Author: User
In this simple loop, how do the variables in the inside write? It's simple, I'm a novice, I don't know how to write.
for ($k =1; $k <4; $k + +) {

$blName _. $k = ' Hello '. $k;



I want to put this $blname_1, the following number 1 is the increment effect, that is: $blName _1, $blName _2, $blName _3, with variable loop instead, how to write it?

------Solution--------------------
for ($k =1; $k <4; $k + +) {
$varname = ' blname_ '. $k;
${$varname} = ' Hello '. $k;

Because $varname = ' blname_1 ' so $ $varname = $blName _1;
Generally not, this should be done with an array of
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, ' hello '. $k);} List ($blName _1, $blName _2, $blName _3) = $arr; 
------Solution--------------------
$var = Array ();
for ($k =1; $k <4; $k + +) {
$var [] = ' hello '. $k;
}
Print_r ($var);
Output
Array (
[0] = ' Hello 1 ';
[1] = ' Hello 2 ';
[2] = ' Hello 3 ';
)
//Take the value $var [1] and the value is ' Hello 2 '.
Read more manuals 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 = ' Hello '. $k;
}

What do you mean by enclosing a variable with {}?
${$varname}
and ${"blname_{$k}"}

curly braces treat the enclosed part as a whole
  • Related Article

    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.