Dynamic variable usage examples in PHP _php tips

Source: Internet
Author: User

The examples in this article describe the dynamic variable usage in PHP. Share to everyone for your reference. The specific analysis is as follows:

Fixed variable defined:

$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"];

Here we use the Loop statement to lose all the values of each variable:

<?php for
($i =1 $i <=8; $i + +)
{ 
 echo "$my _pic_". $i;
> 

This output

<?php
echo "$my _pic_". $i
>

It's definitely not going to work.

Query PHP dynamic variable usage and change to

<?php
for ($i =1 $i <=8; $i + +) {
  $my _pic_var= "$my _pic". $i;//define variable name  
  echo $ $my _pic_var;//Get variable name $my_ The value of Pic_var $ $my _pic_var; 
  The principle of PHP is this $my _pic_var= "$my _pic_1" defines the value
  of the variable//$ $my _pic_var equal to the value of the read $my_pic_1;  
}
? >

I hope this article will help you with your PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.