echo和print_r的奇怪输出

时间:2017-01-06 14:16:56

标签: php

我发现了一些奇怪的行为,无法解释发生了什么:

<!DOCTYPE html>
<html>
<head>
<link href="https://cdnjs.cloudflare.com/ajax/libs/foundation/5.5.2/css/normalize.min.css" rel="stylesheet" type="text/css" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/foundation/5.5.2/css/foundation.min.css" rel="stylesheet" type="text/css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/foundation/5.5.2/js/vendor/modernizr.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/foundation/5.5.2/js/vendor/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/foundation/5.5.2/js/foundation.min.js"></script>
  
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>  
  <div class="row">
    <div class="small-6 columns text">i some text</div>
    <div class="small-6 columns text">i some text</div>
    <div class="small-6 columns text">i some text</div>
    <div class="small-6 columns text">i some text</div>
    <div class="small-6 columns text">i some text</div>
    <div class="small-6 columns text">i some text</div>
    <div class="small-6 columns text">i some text</div>
    <div class="small-6 columns text">i some text</div>
    <div class="small-6 columns text">i some text</div>
    <div class="small-6 columns text">i some text</div>
    <div class="small-6 columns text">i some text</div>
    <div class="small-6 columns text">i some text</div>
  </div>
</body>
</html>

给出:

$e = 3;
echo "with array " . print_r($e);

为什么?

1 个答案:

答案 0 :(得分:4)

在任何输出之前完成连接:

echo "with array " . print_r($e);

  1. 获取print_r的结果。 print_r打印3returns TRUE
  2. with array字符串与TRUE连接。 TRUE的字符串表示形式为1
  3. 摘要:要输出的字符串为3with array 1
  4. 如果您希望print_r 返回值而不是打印它,请使用它的第二个参数:

    echo "with array " . print_r($e, true);  // `with array 3`