麻烦在循环中连接字符串

Dar*_*UFO 1 php string concatenation

我尝试按顺序对我的ZenPhoto安装进行一些更改,以便在相册页面的底部我有一个文本区域,显示上面生成的图像的"嵌入"代码,以便读者可以简单地复制和发布html到他们自己的网站上.

这是代码片段.

<?php $str  = ''; ?>
<?php while (next_image()): ?>
    <div class="imagethumb"><a href="<?php echo html_encode(getImageLinkURL());?>" title="<?php echo getBareImageTitle();?>"><?php printImageThumb(getAnnotatedImageTitle()); ?></a></div>

     $str .= '<a href="<?php echo html_encode(getImageLinkURL());?>" title="<?php echo getBareImageTitle();?>"><?php printImageThumb(getAnnotatedImageTitle()); ?></a>;'
<?php endwhile; ?>

<textarea type="text" size="50">
     <?php echo $str; ?>
</textarea>
Run Code Online (Sandbox Code Playgroud)

我添加的代码是$ str的东西.我正在尝试遍历图像并创建在while循环中的第一个div中使用的html,以便将其作为文本放入str字符串中.这是为库中的每个图像连接的,然后将结束str发布到一个简单的文本区域供用户复制.

我不能让$ str concatination工作.

我是php的新手,我无法完全掌握语法.

任何帮助将非常感激.

Fel*_*ing 5

串联不在<?php标签内.为了便于阅读,您应该使用sprintf:

<?php $str  = ''; ?>
<?php while (next_image()): ?>
    <div class="imagethumb"><a href="<?php echo html_encode(getImageLinkURL());?>" title="<?php echo getBareImageTitle();?>"><?php printImageThumb(getAnnotatedImageTitle()); ?></a></div>

     <?php $str .= sprintf('<a href="%s" title="%s">%s</a>', 
                           html_encode(getImageLinkURL()),
                           getBareImageTitle(),
                           printImageThumb(getAnnotatedImageTitle()));
     ?>
<?php endwhile; ?>
Run Code Online (Sandbox Code Playgroud)

但是你在这里重复一些事情(你创建链接两次).您可以稍微重新构建代码以避免这种情况:

<?php 
   $images = array();
   while (next_image()) {
       $images[] = sprintf('<a href="%s" title="%s">%s</a>', 
                               html_encode(getImageLinkURL()),
                               getBareImageTitle(),
                               printImageThumb(getAnnotatedImageTitle()));
   }
?>
<?php foreach($images as $image): ?>
    <div class="imagethumb"><?php echo $image; ?></div>
<?php endforeach; ?>

<textarea>
     <?php echo implode("", $images); ?>
</textarea>
Run Code Online (Sandbox Code Playgroud)

参考: implode