Viewed   135 times

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?

 Answers

5

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 a 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", not "My foo".

Notes:

  • Before the closing EOT; there should be no spaces or tabs. otherwise you will get an error.
  • The string/tag (EOT) that enclose the text is arbitrary, that is, one can use other strings, e.g. <<<FOO and FOO;
  • EOT : End of transmission, EOD: End of data. [Q]
Thursday, September 15, 2022
4

You need a Ajax call to pass the JS value into php variable

JS Code will be (your js file)

var jsString="hello";
$.ajax({
    url: "ajax.php",
    type: "post",
    data: jsString
});

And in ajax.php (your php file) code will be

$phpString = $_POST['data'];     // assign hello to phpString 
Thursday, November 10, 2022
 
2

alert('my name is: <?php echo $man; ?>' );

Friday, August 19, 2022
 
4

Since you want to remove any horizontal whitespace from a Unicode string you need to use

  • h regex escape ("any horizontal whitespace character (since PHP 5.2.4)")
  • u modifier (see Pattern Modifiers)

Use

$txt = preg_replace("/^h+/mu", '', $txt);

Details

  • ^ - start of a line (m modifier makes ^ match all line start positions, not just string start position)
  • h+ - one or more horizontal whitespaces
  • u modifier will make sure the Unicode text is treated as a sequence of Unicode code points, not just code units, and will make all regex escapes in the pattern Unicode aware.
Saturday, November 12, 2022
34

A potential use-case for ZFS that would see thousands of filesystem mounts would be an organization that uses ZFS for NFS-mounted home directories. Imagine a large company or small university with zpools dedicated to home directory exports. Each user could have a dedicated ZFS filesystem with separate quotas and parameters. If rolling snapshots are implemented, it would be possible for users to see previous versions of their files by descending into the ~/.zfs/snapshot directory. Self-service restores!

Wednesday, December 14, 2022
 
luze
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :