PHP-Echo不';不要打印一些值

PHP-Echo不';不要打印一些值,php,parsing,Php,Parsing,我在PHP中遇到了一个奇怪的问题。 我正在对一个网页进行解析,得到了我要查找的正确值。问题是,如果我想将它们一起打印成一个“echo”,它只打印某些值,而其他值则不打印。我给你们举个例子,这不是我真正的代码,因为我不能在这里通过我的源代码(太长了,我可以做一个简短的例子) 它只打印变量直到变量4,然后什么也不打印。如果我离开就好了 echo(" This is the variable 5: " . $variable5 . " This is the variable 6: " . $vari

我在PHP中遇到了一个奇怪的问题。 我正在对一个网页进行解析,得到了我要查找的正确值。问题是,如果我想将它们一起打印成一个“echo”,它只打印某些值,而其他值则不打印。我给你们举个例子,这不是我真正的代码,因为我不能在这里通过我的源代码(太长了,我可以做一个简短的例子)

它只打印变量直到变量4,然后什么也不打印。如果我离开就好了

echo(" This is the variable 5: " . $variable5 . " This is the variable 6: " . $variable6);
echo(" This is the variable 6: " . $variable6);
它只打印变量5。再说一次,如果你只离开

echo(" This is the variable 5: " . $variable5 . " This is the variable 6: " . $variable6);
echo(" This is the variable 6: " . $variable6);
它正确地打印第六个字符

哪一个可能是问题?另外,我记得我正在对网页进行解析(如果有用的话)。
提前谢谢大家。

我认为你的陈述格式不正确

echo("This is the variable 1: " . $variable1 . " This is the variable 2: " . $variable2 . " This is the variable 3 : . $variable3 . " This is the variable 4: " . $variable4 . " This is the variable 5: " . $variable5 . " This is the variable 6: " . $variable6);
应该是:

echo("This is the variable 1: " . $variable1 . " This is the variable 2: " . $variable2 . " This is the variable 3 " . $variable3 . " This is the variable 4: " . $variable4 . " This is the variable 5: " . $variable5 . " This is the variable 6: " . $variable6);
您可能还想利用PHP的双引号变量扩展。我发现它在像这样的简单情况下很有用

echo("This is the variable 1: $variable1 This is the variable 2: $variable2 This is the variable 3: $variable3 This is the variable 4: $variable4 This is the variable 5: $variable5 This is the variable 6: $variable6");

您只是缺少了语音标记(您最好删除
()
echo
是一种语言结构)

试试这个:

echo "This is the variable 1: " 
. $variable1 . " This is the variable 2: " 
. $variable2 . " This is the variable 3: "
. $variable3 . " This is the variable 4: " 
. $variable4 . " This is the variable 5: " 
. $variable5 . " This is the variable 6: " 
. $variable6 ;

根据@mario的建议,使用插值会更容易,这意味着在双引号内,将解析$variable。如果需要,可以使用
{}
将$variable括起来。

首先。。。如果使用双引号
,则可以执行以下操作:

echo "This is the variable 1: $variable1 
      This is the variable 2: $variable2 
      This is the variable 3: $variable3 
      This is the variable 4: $variable4 
      This is the variable 5: $variable5 
      This is the variable 6: $variable6";
如果要在var或array旁边使用特殊字符,可以执行以下操作:

echo "This is the variable 1: {$variable1} 
      This is the variable 2: {$variable2} 
      This is the variable 3: {$variable3} 
      This is the variable 4: {$variable4} 
      This is the variable 5: {$variable5} 
      This is the variable 6: {$variable6}";
使用
慢,因为解释器在
echo”中查找变量。单报价解决方案类似于以下答案之一:

echo 'This is the variable 1: '
     . $variable1 . ' This is the variable 2: ' 
     . $variable2 . ' This is the variable 3: '
     . $variable3 . ' This is the variable 4: ' 
     . $variable4 . ' This is the variable 5: ' 
     . $variable5 . ' This is the variable 6: '
     . $variable6 ;
差点忘了什么。。。使用它的人不多,而且比上面的示例可读性强得多:

echo 'This is the variable 1: '.$variable1,
     'This is the variable 2: '.$variable2,
     'This is the variable 3: '.$variable3,
     'This is the variable 4: '.$variable4,
     'This is the variable 5: '.$variable5,
     'This is the variable 6: '.$variable6;
尝试改用sprintf


接下来,您只需回显$VarToPrint

你为什么不使用数组来存储…?我不明白下一票。仅仅因为它最终是一个相当简单的解决方案并不意味着它就有理由投反对票。“对于我来说,一个写得很好的问题,加上大量的代码示例和原始研究,你得到了+1的分数。”托马斯凯利同意了,说得好!正确-我建议OP将来使用语法突出显示来避免类似的错误。@RobFarr true,只要看一看问题,语法就会让它变得非常明显。也很好,打破了线,能够看到发生了什么!有没有什么特别的原因让你推荐串联而不是插值?我的意思是OP对语法的理解已经够混乱的了。为什么要避开脚本语言存在的理由——更简单的功能呢?@mario不是真的,只是速度更快,更改更少。现在将编辑它,谢谢@马里奥:别忘了人们在发现插值后通常会问的下一个问题:为什么这不起作用
$a='Hello$username'
printf
如果不想对准备好的字符串执行任何其他操作,听起来会更好。我写错了,现在编辑了它。我现在已经尝试了双引号,但问题仍然存在。谢谢你的回复。现在尝试过,但我不知道为什么,问题仍然存在。变量已正确存储,但未打印。它也不打印变量前的文本。
$VarToPrint = sprintf("This is the variable 1: %s This is the variable 2: %s This is the variable 3: %s This is the variable 4: %s This is the variable 5: %s This is the variable 6: %s", $variable1, $variable2, $variable3, $variable4, $variable5, $variable6);