Note: I'm sorry if this is an extremely simple question but I'm somewhat obsessive compulsive over the formatting of my code.
I have a class that has a function that returns a string that will make up the body text of an email. I want this text formatted so it looks right in the email, but also so it doesn't make my code look funky. Here's what I mean:
class Something
{
public function getEmailText($vars)
{
$text = 'Hello ' . $vars->name . ",
The second line starts two lines below.
I also don't want any spaces before the new line, so it's butted up against the left side of the screen.";
return $text;
}
}
but it could also be written as:
public function getEmailText($vars)
{
$text = "Hello {$vars->name},nrThe second line starts two lines below.nrI also don't want any spaces before the new line, so it's butted up against the left side of the screen.";
return $text;
}
but what's the deal with new lines and carriage returns? What's the difference? Is nn
the equivalent of rr
or nr
? Which should I use when I'm creating a line gap between lines?
Then there's the option of output buffering and heredoc syntax.
How do you deal with using long multiline strings in your objects?
You should use
heredoc
ornowdoc
.The difference between
heredoc
andnowdoc
is that PHP code embedded in aheredoc
gets executed, while PHP code innowdoc
will be printed out as is.In this case
$text
will have the value"My $var"
, not"My foo"
.Notes:
EOT;
there should be no spaces or tabs. otherwise you will get an error.EOT
) that enclose the text is arbitrary, that is, one can use other strings, e.g.<<<FOO
andFOO;