Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Nov 2007
    Posts
    40
    Thanks
    9
    Thanked 0 Times in 0 Posts

    dynamic variable names in a for loop

    I have 6 variables
    $img1 - $img6 and am trying to loop through them one by one.
    From what I found online I thought the code below might work but it does not.
    PHP Code:
    ($x=1$x<7;$x++) { 
    echo 
    "${'img{$x}'}";

    Can anyone help please ?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    PHP Code:
    for($i 1$i <= 6$i++)
    {
        echo ${
    'img' $i} . "<br />\n";

    Like that?

    You should consider using an array instead, it would be far easier and more optimzed:
    PHP Code:
    $aImages = array();
    $aImages[] = '/images/img1.jpg';
    $aImages[] = '/images/img2.jpg';
    $aImages[] = '/images/img3.jpg';
    $aImages[] = '/images/img4.jpg';
    $aImages[] = '/images/img5.jpg';
    $aImages[] = '/images/img6.jpg';


    for (
    $i 0$i count($aImages); $i++)
    {
        echo 
    $aImages[$i] . "<br />\n";
    }
    // Or
    foreach ($aImages AS $image)
    {
        echo 
    $image "<br />\n";

    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    simonwraight (07-14-2008)

  • #3
    New Coder
    Join Date
    Nov 2007
    Posts
    40
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thankyou, I did consider an array but I chose this way as each image path is stored in a database and there is not always 6 in there so the for loop is to check if there is a file path or not.
    Thanks for the help though, I won't pull as much of my hair out now.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    No problem.
    Honestly, if you may not use a magic number (6 in this example) of items, an array is probably a better solution since you can dynamically alter the count for it. I would still recommend the array option (can be pulled from the database in a loop fashion and stored easily) and it will make subsequent checks more usable. The problem is you need to add a check for the existence of the variable if you are choosing to do the magic number route:
    PHP Code:
    for ($i 1$i <= 6$i++)
    {
        if (isset(${
    'img' $i}))
        {
            echo ${
    'img' $i};
        }

    Otherwise you'll get an error trigger indicating that variable img{x} is not a valid variable.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,011
    Thanks
    2
    Thanked 312 Times in 304 Posts
    It takes three to four times longer to reference a variable-variable than to reference an array variable. I tested this a while ago.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •