views:

73

answers:

5

How should I format very long strings in my source code?
I follow the rule, that line of code should not be longer than 80 characters.
(The other rules are Zend Framework formatting standard)

e.g.

protected $_messages = array(
    'key1' => 'very, very long string lorem ipsum dolor sit amet…',
    'key2' => 'this one it very long too, and exceeds 80 characters len…'
);
A: 

It doesn't make sense to limit your variables to a certain length just for readability of code. What are your options? Split it into several variables and concatenate? Pointless extra work (for you and php)

adam
A: 

What adam said. If you really don't want long lines in your code, you can always have the strings in a flat file or db, and read them in at init time.

Peter Loron
+1  A: 
MicE
Thanks. I was trying to concatenate using ' ' . ' ' The proper way is ' " . " ' (strange?)
takeshin
@takeshin: no, you are correct. Sorry for that, it was a typo on my part (already edited it). You can concatenate both "foo"."bar" and 'foo'.'bar', you just need to use the same :o)
MicE
@MicE then, this does not work as expected.
takeshin
@takeshin: I'm afraid that I can confirm that. I just started the server to verified it myself, and I must say that this is a new one for me. It seems that PHP doesn't allow you to use string concatenation in class properties (the same thing seems to apply to strings, not just to arrays). I can only assume that the initial values must not contain any code/operators, otherwise objects cannot be created. If this is the case, I can advise only what adam/Peter suggested, or to keep the initial values empty and set them in the class constructor (where you can concatenate them).
MicE
@takeshin: I've edited the above with a working example. Note that the original version with the typo ( ' lorem "." ipsum' ) actually never concatenated the strings, PHP just didn't throw a parse error since it never saw the concatenation operator.
MicE
@MicE This works, but it is not elegant workaround.
takeshin
+2  A: 

Personally I like long lines on occasion - if you turn off wrapping in your editor it can make things more readable. The breaks in the "string" . "string" . "string"; format is just messy imo.

Rules are there to be broken, essentially. Use whatever is most readable for you rather than conforming to someone else's idea of readable.

DCD
A: 

if the string is reeeeeeealy long, you can always use $var = include('file.php'); and inside that file <?php return "loooooong string";

Anyways I think that the rule is meant for INTERACTIVE lines. Means you should never have long like like this:

// dont use
$_string = "this is a long " . $string . " which is generated by program code along with some parameters like: " . $parameter1 . " or " . $parameter2;
// dont use
$_string = "The apples are "($paramBool ? "" : $not) . " there. The amount of apples is " . ($paramInt > 3 ? $bigger : $smaller) . "than 3";
// you can use IMHO
$_string = "this is a long string which is generated by program code along with some parameters like: width or height";

The first and second should not be used because you can easily overlook the variables or get lost in brackets. On the other hand the third has no variables, functions or any other logic inside, so there is little chance that you will ever need to write into that. And you can use you IDEs line wrap for that... as long as other "dynamic and interactive" lines are kept below 80.

But this is only my personal preference and the "de jure" answer is in the MicE's post.

Tomáš Fejfar