1

我在 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

4 回答 4

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 的建议,使用插值会更容易,这意味着在双引号内 $variable 将被解析。如果需要,使用{}包围 $ 变量。

于 2013-11-10T18:31:13.353 回答
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;
于 2013-11-10T18:43:38.623 回答
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");
于 2013-11-10T18:31:06.353 回答
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;

于 2013-11-10T18:45:59.597 回答