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

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

WBOY
Release: 2016-06-13 13:38:55
Original
1097 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();<br>for( $k=1; $k $var[] = '你好'.$k;<br>}<br>print_r($var);<br>// 输出<br>array(<br>    [0] => '你好1';<br>    [1] => '你好2';<br>    [2] => '你好3';<br>)<br>// 取值用 $var[1] , 值为 '你好2'.<br>多看看手册为好 http://www.php.net/manual/zh/language.types.array.php
<br><font color="#e78608">------解决方案--------------------</font><br>${"blName$k"} = '你好'.$k;<br><br>php 推荐语法<br>$varname = 'blName';<br>for( $k=1; $k $blName = $varname . $k;<br> $$blName = '你好'.$k; <br>}<br><br>请问下用{ }括起变量是什么意思?<br>${$varname}<br>和 ${"blName_{$k}"}<br><br>花括号将被括起的部分视为一个整体 <div class="clear">
                 
              
              
        
            </div>
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