dtqpw68806 2014-02-19 06:04
浏览 45
已采纳

PHP - 字符串在迭代for循环内没有正确连接

I wrote a simple script to display content to the user in the requested order using 4 arrays of equal length built concurrently, but for some reason the content string built from this is only being concatenated with the final item in the array regardless of the number of requests.

This is the part of my code that is causing the issue:

$snippet="<td id = \"content\">";
for($i=0;$i<count($content_types);$i++){
    $type = $content_types[i];
    if($type == "Video")
    {
        $pos_str = ((strlen($content_pos[i]) > 0) ? "class=\"".$content_pos[i]."\"" : "");
        $file_type = $content_file_types[i];
        $snippet .= "<video ".$pos_str." width=\"320\" height=\"240\" controls>
                            <source src=\"".$content[i]."\" type=\"video/".$file_type."\">
                            Your browser does not support the video tag.
                        </video>";
    }
    if($type == "Audio")
    {
        $pos_str = ((strlen($content_pos[i]) > 0) ? "class=\"".$content_pos[i]."\"" : "");
        $file_type = $content_file_types[i];
        $snippet .= "<audio ".$pos_str." controls>
                            <source src=\"".$content[i]."\" type=\"audio/".$file_type."\">
                            Your browser does not support the audio tag.
                        </audio>";
    }
    if($type == "Text")
    {
        $snippet .= "<p class=\"content_text\">
                    ". urldecode($content[i]) ."
                </p>";
    }
    if($type == "Image")
    {
        $pos_str = ((strlen($content_pos[i]) > 0) ? "class=\"".$content_pos[i]."\"" : "");
        $snippet .= "<img src=\"
                    ".$content[i]."
                \" ".$pos_str."></img>";
    }
}
$snippet .="</td>";

Each code segment for the different content types works correctly, it is only the concatenation of these values into a single string that is not functioning.

The two external '$snippet' concatenations that occur are represented in the output along with the final concatenation within the loop.

I've been working on this for hours and have not been able to figure out why only the final item is being added to the string.

I'm probably just too close to the code and therefore unable to see the obvious mistake that I've made.

Any help would be appreciated.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dongyuan1970 2014-02-19 06:11
    关注

    You missing $ symbol at all the places you have used variable $i.

    $type = $content_types[$i];
                           ^
    

    Note: Add error_reporting(E_ALL & E_STRICT); on top of the file it will show you errors & notices in such cases.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错
  • ¥15 能把水桶搬到饮水机的机械设计
  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器
  • ¥15 关于#爬虫#的问题:请问HMDB代谢物爬虫的那个工具可以提供一下吗
  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。
  • ¥15 孟德尔随机化结果不一致