开发者

How to format long strings in arrays

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…'
);


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.


If you cannot store the values in a DB/file (as per adam/Peter), and you absolutely need to keep the 80/120 character limit as per Zend Framework guidelines, then you can adhere to them even in this case.

As per: http://framework.zend.com/manual/en/coding-standard.coding-style.html

String Concatenation (one level extra indentation)

$sql = "SELECT `id`, `name` FROM `people` "
    . "WHERE `name` = 'Susan' "
    . "ORDER BY `name` ASC ";

Associative Arrays (one level extra indentation)

$sampleArray = array(
    'firstKey'  => 'firstValue',
    'secondKey' => 'secondValue',
);

The above two combined (two level extra indentation for long strings)

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…'
);

Edit:
The above does not work (thanks & sorry takeshin), as PHP does not seem to allow any code/operators in initial values of class properties.

The solution is to concatenate and set the initial values in class constructor:

<?php

class bar {
    protected $_messages = Array();

    public function __construct() {

        // manually initialize, or load from DB/XML/etc
        $this->_messages[] = "very, very long string "
            . "lorem ipsum dolor sit amet";

        $this->_messages[] = "this one it very long too, "
            . "and exceeds 80 characters len";

        var_dump( $this->_messages );
    }
}

$foo = new bar();

?>


As of PHP 5.4 this does not seem to be an issue. I had some code that routinely handled 5000+ character strings, passing them as array entries without issue in 5.4. However, when migrating back to 5.3 due to a server change, I encountered a roadblock where the array could not be defined with a superlong string.

My problem is somewhat different, and perhaps this is helpful, perhaps not. If your app wants an array for its DB params, and an object just won't cut it, then you might be able to include your string as part of the SQL statement instead, until you can get onto 5.4.

EDIT: As an added note, our server admin upgraded us to 5.4 per my (and my team's) request, and it did, in fact, correct the issue of holding a long string as an array element.


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)


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.


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.


If line breaks are allowed you can use Nowdoc

public $arr = array(
  <<<TEXT
Some very
long text
TEXT
,
'key2' => <<<TEXT
Some more very
long text
TEXT
);

But looks bad!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜