0

this is my first post and I am new to this community. I'm currently learning php, but facing an issue with paragraphs:

<?php
$name = "Erik Mustermann";
echo $name . "<br />";
echo strlen($name) . "<br />";
var_dump($name) . "<br />";
echo "Heyho" . "<br />";
var_dump($name) . "<br />";
?>

output:

Erik Mustermann
15
string(15) "Erik Mustermann" Heyho
string(15) "Erik Mustermann"

Why is the string "Heyho" in the same line like var_dump even I created a paragraph?

  • Because, `var_dump()` isn't an HTML interpreter, it just dumps code. http://php.net/manual/en/function.var-dump.php so you got back exactly what you asked PHP to do. – Funk Forty Niner Jul 30 '17 at 13:02
  • Yes, that's right, but why is my string displayed in the same line even I wrote "
    "?
    –  Jul 30 '17 at 13:07
  • 1
    Hint: Take a look at your HTML source. You'll see the breaks. – Funk Forty Niner Jul 30 '17 at 13:13
  • 1
    http://php.net/manual/en/function.var-dump.php --- *"As with anything that **outputs its result directly to the browser,** the output-control functions can be used to capture the output of this function, and save it in a string (for example)."*. – Funk Forty Niner Jul 30 '17 at 13:16
  • 2
    Because you're not writing the `
    ` string to output where you intended to. Try `var_dump($name); echo "
    ";` instead of `var_dump($name) . "
    ";` and have a look at the raw output (i.e by pressing `ctrl + u` if you're viewing the output in a web browser) to see the difference.
    – Nima Jul 30 '17 at 14:33

1 Answers1

1

To be honest I'm surprised this code compiles.

With the code var_dump($name) . "<br />"; the second part . "<br />"; is not passed to var_dump and so it isn't output. What you want is:

var_dump($name . "<br />");

echo is not a function but a language construct which is why it doesn't required the braces.

As a side note, <br/> doesn't create a new paragraph, it creates a new line.

lewis
  • 2,936
  • 2
  • 37
  • 72
  • Alright, I'm getting closer. With this method my output is:
    string(21) "Erik Mustermann
    "
    –  Jul 30 '17 at 14:06