# Hanging semicolons in multiline statements

I'm writing code to do line printing to a template -- counting characters to box A, then to box B, etc. -- and as such, it's not very modular. There are a lot of (long) variable names and each statement, which adds to the string to be printed, is pretty long.

So I've been breaking up the statements across multiple lines. But I don't know how I should be indenting it or where I should put the semicolon.

Here's a sample line:

$rbt .= "\n" . str_repeat(" ",$rbtSt_billToBox_leadingWidth)
. padBothAndTruncate($statementInfoArray['bill-to-addr1'],$rbtSt_billToBox_addr_maxWidth)
. str_repeat(" ", $rbtSt_billToBox_innerMargin) . substr($statementInfoArray['bill-to-code'], 0, $rbtSt_billToBox_acctNum_maxWidth) ;  I like how the concat operators are lined up and how it looks bracketed, but the naked semicolon on the following line makes me a little nervous. Putting it at the end of the line makes me even more nervous, since then it's hard to see. It gets a little trickier when there's more nesting. $rbt .= "\n"
. str_repeat(" ", $rbtSt_billToBox_leadingWidth) . padBothAndTruncate($statementInfoArray['bill-to-city'] . ", " . $statementInfoArray['bill-to-state'] . " " .$statementInfoArray['bill-to-zip']
)
;


Do I always do multiple lines? Just when it is more than a certain number of characters (say, 120)? Once a statement is multiline, should I only have one operator per line?

I'm feeling a bit paralyzed by choices here and I fear that none of them are very good.

• One thing to keep in mind is that this all comes down to convention and personal preference. Essentially, no way is right or wrong, and it's not like your code won't work if you write it in a certain way. Relax and try to find what looks best to you. – Ivo Coumans Nov 27 '14 at 9:05
• This looks like just sample codes. This not appropriate here – morbidCode Nov 27 '14 at 10:13
• They're not sample codes - they're from a real program I'm working on. I cannot post the entire thing as it's a closed-source project, sorry. – AmadeusDrZaius Nov 27 '14 at 10:13

This is indeed purely personal, however, when you work in a group, or want to share your code, it's best to stick to certain conventions. A good source for this is:

http://www.php-fig.org/psr/psr-2

Eventhough these guidelines are quite good, I don't always follow them myself. For instance, I often use two spaces to indent, not four. Etc.

As for my personal opinion about any code: It should be very easy to read and understand. Your code wouldn't be accepted here, mainly due to the lack of comments and sometimes vague names. But that's not what your question is about. As for the concatenation dots, I like them at the end of lines:

$startOfLine = PHP_EOL. str_repeat(' ',$rbtSt_billToBox_leadingWidth);
$innerMargin = str_repeat(' ',$rbtSt_billToBox_innerMargin);
extract($statementInfoArray,'stm');$rbt .= $startOfLine. padBothAndTruncate($stm_bill-to-addr1,$rbtSt_billToBox_addr_maxWidth).$innerMargin.
substr($stm_bill-to-code,0,$rbtSt_billToBox_acctNum_maxWidth).
$startOfLine. padBothAndTruncate($stm_bill-to-city.', '.$stm_bill-to-state.' '.$stm_bill-to-zip);


and then I align all the parts on the left side. This makes it very clear what belongs together. I am however sure there are many other ways to do this.

• "mainly due to the lack of comments and sometimes vague names." I apologize for that - it's more clear what's going on with all of the code, but I wanted to post a MWE just focusing on the semicolons / indentation. Thanks for the suggestions. – AmadeusDrZaius Nov 27 '14 at 10:16
• Yes, I assumed that might have been the case. – KIKO Software Nov 27 '14 at 10:39