Home > php教程 > php手册 > php中动态变量用法实例,php变量用法实例

php中动态变量用法实例,php变量用法实例

WBOY
Release: 2016-06-13 09:02:01
Original
787 people have browsed it

php中动态变量用法实例,php变量用法实例

本文实例讲述了php中动态变量用法。分享给大家供大家参考。具体分析如下:

定义的固定变量:

$my_pic_1=$row["pic_1"];
$my_pic_2=$row["pic_2"];
$my_pic_3=$row["pic_3"];
$my_pic_4=$row["pic_4"];
$my_pic_5=$row["pic_5"];
$my_pic_6=$row["pic_6"];
$my_pic_7=$row["pic_7"];
$my_pic_8=$row["pic_8"];

Copy after login

这里我们用循环语句把每个变量的值全部输出来:

<&#63;php
for ($i=1;$i<=8;$i++)
{ 
 echo "$my_pic_".$i; 
}
&#63;> 

Copy after login

这样输出

<&#63;php
echo "$my_pic_".$i;
&#63;>
Copy after login

肯定是不行的。

查询php动态变量用法后改成

<&#63;php
for ($i=1;$i<=8;$i++){
  $my_pic_var="$my_pic".$i; //定义变量名  
  echo $$my_pic_var; //获得变量名$my_pic_var的值$$my_pic_var; 
  //PHP的原理是这样的 $my_pic_var="$my_pic_1"定义变量值
  //$$my_pic_var等于读取$my_pic_1的值;  
}
&#63;>

Copy after login

希望本文所述对大家的php程序设计有所帮助。

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template