tags:

views:

250

answers:

6

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},\n\rThe second line starts two lines below.\n\rI 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 \n\n the equivalent of \r\r or \n\r? 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?

+4  A: 

You should use HEREDOC or NOWDOC.

$var = "some text";
$text = <<<EOT 
  Place your text between the EOT. It's
  the delimiter that ends the text
  of your multiline string.
  $var
EOT;

The difference between Heredoc and Nowdoc is, that php code embedded in an heredoc gets executed, while php code in Nowdoc will be printed out as is.

$var = "foo";
$text = <<<'EOT'
  My $var
EOT;

In this case $text will have the value: "My $var".

Best wishes, Fabian

halfdan
Wouldn't your example go wrong? Using the term EOT 3 times?
powtac
Why? Because everything between the first EOT and the second is considered to be a string as-is. Which means you don't need to put backslash n all over your multi-line strings.@Fabian: What is the difference between a HEREDOC and NOWDOC, or are they the same thing?
Kenny Drobnack
@powtac: No, because it has to be on a new line to end a heredoc. Specifically, it has to be `EOT;` with no spaces before it.
R. Bemrose
I have added an explanation for Nowdoc.
halfdan
Looks like StackOverflow's syntax highlighter needs to learn HEREDOC...
Franz
Fabian,Thanks for the explanation of NOWDOC. It's been a while since I've done PHP. I mostly do Perl now and have used HEREDOC quite a few times. I've never even heard of NOWDOC before. Have to see if Perl supports it.
Kenny Drobnack
+1  A: 

Adding \n and/or \r in the middle of the string, and having a very long line of code, like in second example, doesn't feel right : when you read the code, you don't see the result, and you have to scroll.

In this kind of situations, I always use Heredoc (Or Nowdoc, if using PHP >= 5.3) : easy to write, easy to read, no need for super-long lines, ...

For instance :

$var = 'World';
$str = <<<MARKER
this is a very
long string that
doesn't require
horizontal scrolling, 
and interpolates variables :
Hello, $var!
MARKER;

Just one thing : the end marker (and the ';' after it) must be the only thing on its line : no space/tab before or after !

Pascal MARTIN
+3  A: 

I use templates for long text:

email-template.txt contains

hello {name}!
how are you?

In PHP I do this:

$email = file_get_contents('email-template.txt');
$email = str_replace('{name},', 'Simon', $email);
powtac
interesting...where do you store your templates in the directory structure of a web application?
Andrew
My code is just a demo. For a huge web app you might use a structure like /templates/emails/ , /templates/userfeedback/. But if you have more stuff I would recommend a full template system like SMARTY.
powtac
Dwoo http://dwoo.org/ is a decent SMARTY-compatible PHP templating system which some consider a good successor (started on PHP5, so no PHP4 baggage).
micahwittman
A: 

In regards to your question about newlines and carriage returns:

I would recommend using the predefined global constant PHP_EOL as it will solve any cross-platform compatibility issues.

This question has been raised on SO beforehand and you can find out more information by reading "When do I use the PHP constant PHP_EOL"

cballou
The comment to this answer suggests that I should use \r\n for new lines in emails: http://stackoverflow.com/questions/128560/when-do-i-use-the-php-constant-phpeol/699754#699754
Andrew
+1  A: 

Sure, you could use HEREDOC, but as far as code readability goes it's not really any better than the first example, wrapping the string across multiple lines.

If you really want your multi-line string to look good and flow well with your code, I'd recommend concatenating strings together as such:

$text = "Hello, {$vars->name},\r\n\r\n"
    . "The second line starts two lines below.\r\n"
    . ".. Third line... etc";

This might be slightly slower than HEREDOC or a multi-line string, but it will flow well with your code's indentation and make it easier to read.

pix0r
+1  A: 

I use similar system as pix0r and I think that makes the code quite readable. Sometimes I would actually go as far as separating the line breaks in double quotes and use single quotes for the rest of the string. That way they stand out from the rest of the text and variables also stand out better if you use concatenation rather than inject them inside double quoted string. So I might do something like this with your original example:

$text = 'Hello ' . $vars->name . ','
      . "\r\n\r\n"
      . 'The second line starts two lines below.'
      . "\r\n\r\n"
      . '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;

Regarding the line breaks, with email you should always use \r\n. PHP_EOL is for files that are meant to be used in the same operating system that php is running on.

Squall
Thanks for the heads up on email line breaks.
Andrew