Re: Variable representation

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 02-04-2012 07:15, tamouse mailing lists wrote:
As for doing what you originally asked, that requires doing an eval()
on the statement utilizing string interpolation, like so:

   eval('echo "image $i is $image_' . $i . '".PHP_EOL;');

but I think that's a bit harder to read and understand what's going
on. When you have to add in escaped quotes and such, it gets much
hairier.

To utilize it in the loop you have above, I'd split the echoes up:

    echo "<li><a href=\"http://www.theverseoftheday.info/store-images/";;
    eval ('echo "$image_" . $i;');
    echo "\" title=\"Image " . $i . "\">Image " . $i . "</a></li>\r\n";

so that the eval portion is doing only what needs to be interpolated
and evaled. The rest of the output is fine the way it is.

Note that if you did this:

    echo "<li><a href=\"http://www.theverseoftheday.info/store-images/";
. eval('echo "$image_" . $i;') . "\" title=\"Image " . $i . "\">Image
" . $i ."</a></li>\r\n";

the part in the eval would get written out first, then the rest of the
echoed string, which is why you would need to split them up first.

Generally, I think it's best to completely avoid using eval unless
there is no other way to do what you want.

Usually if you think you need to use eval: think again. In this case, it again holds true.

Instead of doing what you do, you can also reference the variable as:
echo ${'image_'.$i};
or
echo $GLOBALS['image_'.$i];

Both are preferable by far over using eval, with all its potential security concerns.

As for the original threat-author's request. I agree with you that a simple bit of code as below should work fine:
foreach(range(1,4) as $i) {
   if(strlen($img=trim($row['image_'.$i])) > 0) {
      echo '<li>',
             '<a href="http://example.com/path/'.$img.'">',
               'Image '.$i,
             '</a>',
           '</li>',
           PHP_EOL;
   }
}

[and yes, I prefer using comma notation in echo to split it into clear, readable parts]
- Tul

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux