Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-字符串未在迭代for循环内正确连接_Php_String_Concatenation_String Concatenation - Fatal编程技术网

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

PHP-字符串未在迭代for循环内正确连接,php,string,concatenation,string-concatenation,Php,String,Concatenation,String Concatenation,我编写了一个简单的脚本,使用4个并行构建的等长数组,按照请求的顺序向用户显示内容,但出于某种原因,由此构建的内容字符串仅与数组中的最后一项连接,而与请求的数量无关 这是我的代码中导致问题的部分: $snippet="<td id = \"content\">"; for($i=0;$i<count($content_types);$i++){ $type = $content_types[i]; if($type == "Video") {

我编写了一个简单的脚本,使用4个并行构建的等长数组,按照请求的顺序向用户显示内容,但出于某种原因,由此构建的内容字符串仅与数组中的最后一项连接,而与请求的数量无关

这是我的代码中导致问题的部分:

$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>";
$snippet=”“;
对于($i=0;$i=0)?“class=\”.$content\u pos[i]。“\”:”;
$file_type=$content_file_types[i];
$snippet.=”
您的浏览器不支持视频标记。
";
}
如果($type==“Audio”)
{
$pos\u str=((strlen($content\u pos[i])>0)?“class=\”.$content\u pos[i]。“\”“:”);
$file_type=$content_file_types[i];
$snippet.=”
您的浏览器不支持音频标记。
";
}
如果($type==“Text”)
{
$snippet.=“

“.urldecode($content[i])”

”; } 如果($type==“Image”) { $pos\u str=((strlen($content\u pos[i])>0)?“class=\”.$content\u pos[i]。“\”“:”); $snippet.=“”; } } $snippet.=“”;
不同内容类型的每个代码段都能正常工作,只有将这些值串联成一个字符串时才起作用

发生的两个外部“$snippet”连接与循环中的最终连接一起在输出中表示

我已经为此工作了好几个小时,但一直无法弄清楚为什么只将最后一项添加到字符串中

我可能只是太接近代码,因此看不到我犯的明显错误

任何帮助都将不胜感激


提前感谢。

您在使用变量
$i
的所有位置都缺少
$
符号

$type = $content_types[$i];
                       ^

注意:添加
错误报告(E\u全部&E\u严格)在文件顶部,它将显示此类情况下的错误和注意事项。

就是这样。非常感谢你。我知道我错过了一些简单的东西。谢谢你关于错误报告的建议。非常有帮助。如果我的答案解决了您的问题,请单击大复选框接受它作为答案。对此表示抱歉。这是我第一次在这里发布问题,我不知道这是如何将问题标记为已回答。再次感谢。