Home > Backend Development > PHP Tutorial > 这个简单的循环,里面的变量到底怎么写呢?很简单,小弟我是新手不知道写

这个简单的循环,里面的变量到底怎么写呢?很简单,小弟我是新手不知道写

WBOY
Release: 2016-06-13 10:13:32
Original
903 people have browsed it

这个简单的循环,里面的变量到底如何写呢?很简单,我是新手不知道写啊
for( $k=1; $k
  $blName_.$k = '你好'.$k; 



// 意思是我想把这个$blName_1 ,后面的数字1是递增的效果,就是:$blName_1,$blName_2,$blName_3, 用变量循环代替,该如何写呢?

------解决方案--------------------
for( $k=1; $k $varname = 'blName_'.$k;
${$varname} = '你好'.$k; 

// 因为 $varname = 'blName_1' 所以 $$varname = $blName_1;
一般都不会这么干,这种情况应该用数组

PHP code
http://www.php.net/manual/zh/language.variables.variable.php<br><font color="#e78608">------解决方案--------------------</font><br>
Copy after login
PHP code
for( $k=1; $k<font color="#e78608">------解决方案--------------------</font><br>
Copy after login
PHP code
$arr=array();for( $k=1; $k<font color="#e78608">------解决方案--------------------</font><br>$var = array();
for( $k=1; $k $var[] = '你好'.$k;

print_r($var);
// 输出
array(
[0] => '你好1';
[1] => '你好2';
[2] => '你好3';
)
// 取值用 $var[1] , 值为 '你好2'.
多看看手册为好 http://www.php.net/manual/zh/language.types.array.php
------解决方案--------------------
${"blName$k"} = '你好'.$k;

php 推荐语法
$varname = 'blName';
for( $k=1; $k $blName = $varname . $k;
$$blName = '你好'.$k;
}

请问下用{ }括起变量是什么意思?
${$varname}
和 ${"blName_{$k}"}

花括号将被括起的部分视为一个整体
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template