Prevent PHP Interpreting New Lines in Strings in the Source Code -
i have assign big strings variables. in source code preferably want maintain lines within 80 characters.
ideally want able lay these literal strings out on multiple lines.
what want avoid using concatenation, or function calls (e.g. preg_replace()
), bring together multiple strings in one. don't thought have invoke language features in order improve style of code.
example of like:
$text = <<<text line 1 line 2 line 3 text; echo($text);
this should output:
line1line2line3
is possible?
there few options:
just concatenate (preferred)
use array
constructs
use sprintf()
just concatenate:
echo 'long long line1' . 'another long line 2' . 'the lastly long line 3';
what efficiency?
the above code compiles next opcodes (which what's run):
5 0 > concat ~0 'long+long+line1', 'another+long+line+2' 1 concat ~1 ~0, 'the+last+very+long+line+3' 2 echo ~1
as can see, builds string concatenating first 2 lines, followed lastly line; in end ~0
discarded. in terms of memory, difference negligible.
this single echo
statement like:
3 0 > echo 'long+long+line1another+long+line+2the+last+very+long+line+3'
technically it's faster because there no intermediate steps, in reality won't sense difference @ all.
using array
:
echo join('', array( 'line 1', 'line 2', 'line 3', ));
using sprintf()
:
echo sprintf('%s%s%s', 'line 1', 'line 2', 'line 3' );
php string coding-style string-formatting code-formatting
No comments:
Post a Comment