0

I have some PHP code that I want to convert to a function. Unfortunately the code uses nowdoc to create a format string for a sprintf() call. This poses a formatting dilemma because It means I can't indent the closing identifier to match the function layout. The nowdoc is an HTML snippet so I could remove all line breaks and treat it as a simple string variable, but that makes the code more difficult to read. Another way would be to create the format string in global scope using nowdoc, and passing the resulting string to the function. Is there any way to use nowdoc and keep my normal formatting style?

wm_g
  • 25
  • 5
  • The end identifier has to be at the beginning of the line, your problem is even mentioned on http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc – Progman Aug 11 '18 at 20:49
  • I read the manual, and understand that the end identifier and semicolon are all that's permitted on that line, which is why I said "I can't indent the closing identifier to match the function layout." I don't believe I'm the first one to need a multi-line format string inside a function, so I was hoping someone had found an alternative. As it is I ended up using a bunch of .= operators. It's not perfect, but it keeps the HTML text neatly organized, and readable. – wm_g Aug 11 '18 at 22:50
  • Creating a template file and using file_get_contents() works too. It requires creating a text file, then reading it into a string variable to use as the format string for sprintf(). It's a little more complicated in some ways, cleaner code in others. – wm_g Aug 12 '18 at 01:08
  • You should be able to just use `include()` and include the file in the function, keeping required indentation in the included HEREDOC file, but also normal indenting in the function – Rasclatt Aug 12 '18 at 01:21

1 Answers1

0

As mentioned in my comment, if you want to keep your formatting on both pages, just use include():

/string.php

<?php
$str = <<<'EOD'
This is a string
EOD;
// This line after "EOD;" needs something on it or it may throw this error:
// Parse error: syntax error, unexpected end of file, expecting variable (T_VARIABLE) or heredoc end (T_END_HEREDOC) or ${ (T_DOLLAR_OPEN_CURLY_BRACES) or {$ (T_CURLY_OPEN)

/myfunction.php

<?php
function myfunction()
{
    # Include the nowdoc file
    include(__DIR__.'/string.php');
    # Return the string back
    return $str;
}
Rasclatt
  • 12,498
  • 3
  • 25
  • 33
  • I experimented with include, but I must have gotten something wrong because I couldn't get it to work. I ended up using file_get_contents(). The basic concept is the same, but I think include() may have advantages. I will play with include again and see what I did wrong. Thanks. – wm_g Aug 12 '18 at 04:18
  • I would maybe try and get include to work because file_get_contents() doesn’t allow the php (if there happens to be any) to run. Specially, variables won’t work inside the NOWDOC. – Rasclatt Aug 12 '18 at 15:04
  • That's what I said, I need to see what I did wrong with the include() because it has advantages over file_get_contents() – wm_g Aug 12 '18 at 16:14