PHP - Echo不打印某些值

时间:2013-11-10 18:29:11

标签: php parsing

我在PHP中遇到了一个奇怪的问题。 我正在解析一个网页,我得到了正在寻找的正确值。问题是,如果我想打印它们,一起形成一个“回声”,它只打印某些值,而其他值不打印。我举个例子,这不是我真正的代码,因为我不能在这里找到我的来源(这是很长的,我可以做一个较短的例子)。

<?php
 $variable1 = function_to_get_this_variable;
 $variable2 = function_to_get_this_variable;
 $variable3 = function_to_get_this_variable;
 $variable4 = function_to_get_this_variable;
 $variable5 = function_to_get_this_variable;
 $variable6 = function_to_get_this_variable;

现在,如果我单独打印每个值(即通过echo $ variable1;),我得到我正在寻找的值。但如果尝试将它们打印在一起,通过

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);

它只打印变量直到变量4,然后它不会打印任何内容。如果我只离开

echo(" This is the variable 5: " . $variable5 . " This is the variable 6: " . $variable6);

它只打印变量5.再次,如果只留下

echo(" This is the variable 6: " . $variable6);

正确打印第六个。

哪个可能是问题?此外,我记得你,我正在解析一个网页(如果它有用)。 提前全部谢谢。

4 个答案:

答案 0 :(得分:2)

您只是缺少语音标记(您也可以删除()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的建议,您可以更轻松地使用插值,这意味着在双引号内将解析$变量。如果需要,请使用{}包围$变量。

答案 1 :(得分:1)

首先......如果你使用双引号",你可以这样做:

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 '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;

答案 2 :(得分:0)

我认为你的陈述是错误的......

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");

答案 3 :(得分:0)

尝试使用 sprintf 而不是

$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);

接下来你只需 echo $ VarToPrint;

相关问题