Home >Backend Development >PHP Tutorial > php定义变量,该怎么处理

php定义变量,该怎么处理

WBOY
WBOYOriginal
2016-06-13 13:18:40875browse

php定义变量
想循环定义7个变量,变量名分别为 $var1,$var2,$var3,……,var7
应该怎么写?
不要一个一个定义,想用个循环
for($i=1;$i    
}

------解决方案--------------------

PHP code
[liangdong@bb-browser-test00.vm.baidu.com php_project]$ cat main.php 
<?php function make_variables($prefix, $start, $end) {
        while ($start <= $end) {
                $var_name = $prefix . $start;
                global $$var_name;
                ++ $start;
        }
}

make_variables('var', 1, 7);
for ($i = 1; $i <= 7; ++ $i) {
        $var_name = 'var' . $i;
        echo $$var_name;
}
?> <div class="clear">
                 
              
              
        
            </div>
Statement:
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