1

Consider below code :

<?php
function a($txt) {
    b("Cleveland");
}
function b($txt) {
    var_dump(debug_backtrace());
}
echo '<pre>';
a("Peter");
'</pre>';
?>

Output :

array(2) {
  [0]=>
  array(4) {
    ["file"]=>
    string(40) "C:\xampp\htdocs\php_playground\hello.php"
    ["line"]=>
    int(6)
    ["function"]=>
    string(1) "b"
    ["args"]=>
    array(1) {
      [0]=>
      string(9) "Cleveland"
    }
  }
  [1]=>
  array(4) {
    ["file"]=>
    string(40) "C:\xampp\htdocs\php_playground\hello.php"
    ["line"]=>
    int(12)
    ["function"]=>
    string(1) "a"
    ["args"]=>
    array(1) {
      [0]=>
      string(5) "Peter"
    }
  }
}

Now, consider below code :

<?php
function a($txt) {
    b("Cleveland");
}
function b($txt) {
    var_dump(debug_backtrace());
}
echo '<pre>'.a("Peter").'</pre>';
?>

Output :

array(2) { [0]=> array(4) { ["file"]=> string(40) "C:\xampp\htdocs\php_playground\hello.php" ["line"]=> int(3) ["function"]=> string(1) "b" ["args"]=> array(1) { [0]=> string(9) "Cleveland" } } [1]=> array(4) { ["file"]=> string(40) "C:\xampp\htdocs\php_playground\hello.php" ["line"]=> int(8) ["function"]=> string(1) "a" ["args"]=> array(1) { [0]=> string(5) "Peter" } } } 

Why I'm getting two different outputs for the two different usages of <pre> tag?

echo '<pre>';
a("Peter");
'</pre>';

AND

echo '<pre>'.a("Peter").'</pre>';

I think I'm doing nothing different in both of the statements. I'm doing almost the same thing then why I'm getting different outputs?

  • `'';` does **exactly** nothing. You can put instead `'';` or just `'bla';`, it's the same. It is a string lost in the PHP code, an expression without any effect. – axiac Dec 08 '17 at 15:49

2 Answers2

2

echo only runs once it's finished building the string. So if you run

function a() {
  echo 'A';
}
echo 'Hello ' . a();

you'll end up with the output

AHello

This is because the function is evaluated as part of building up the output to the echo call. If the function itself has an echo statement (or var_dump, print_r, etc), then it will be shown first.

There are two options to resolve this:

1) Refactor your function to return the string instead

function a() {
  return 'A';
}
echo 'Hello ' . a();

2) Call your function as part of a separate echo call, using commas instead of string concatenation:

function a() {
  echo 'A';
}
echo 'Hello ' , a();

In both of these cases you'll end up with the correct output:

Hello A

Specifically for your question, the output you're seeing is the output from var_dump, followed by <pre></pre>, which won't show up at all. (Or actually, as many people have mentioned now, just <pre>. Although this won't have been making much difference to the behaviour.)

iainn
  • 16,826
  • 9
  • 33
  • 40
1

echo outputs to STDOUT. var_dump outputs to STDOUT.

echo '<pre>';
a("Peter");
'</pre>';

This outputs <pre>.

Then it outputs the var_dump.

Then it waves the string literal </pre> around in the air doing nothing with it.


echo '<pre>'.a("Peter").'</pre>';

This starts building a string.

It starts with <pre>.

Then it outputs the result of var_dump, and joins the return value of the a function (which doesn't have a return statement) to the string.

Then it joins </pre> to the string.

Finally it outputs the string (<pre></pre>)

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335