Detailed explanation of how to use dynamic variables in php

怪我咯
Release: 2023-03-12 16:20:02
Original
1557 people have browsed it

This article mainly introduces the usage of dynamic variables in php. The examples analyze the related usage skills of dynamic variables in php. Friends in need can refer to the following

The examples of this article describe php Dynamic variable usage. Share it with everyone for your reference. The specific analysis is as follows: Fixed variables defined by

:

$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

Here we use the

loop statement to output all the values ​​of each variable:

<?php
for ($i=1;$i<=8;$i++)
{ 
 echo "$my_pic_".$i; 
}
?>
Copy after login

It is definitely not possible to output

<?php
echo "$my_pic_".$i;
?>
Copy after login

like this.


Query php dynamic variable usage and change it to

<?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的值;  
}
?>
Copy after login

The above is the detailed content of Detailed explanation of how to use dynamic variables in php. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!