I want to have a string variable for a PHP class, which would be available to all methods.
However, this variable is quite long, so I want to separate it into multiple lines.
For example,
$variable = "line 1" .
"line 2" .
"line 3";
But above doesn’t work.
I tried EOD, but EOD is not allowed within class. And when I declare it outside the class, I can’t access the variable from within the class.
What is the best way?
If you are using PHP >= 5.3, you could use HEREDOC syntax to declare your string :
But this :
., will not work : it’s done at execution time.And another drawback is that this will put newlines in the string — which might, or not, be a bad thing.
If you are using PHP <= 5.2 :
You can’t do that ; a solution could be to initialize the string in your class’ constructor :
(same not with newlines)
Or, here, you can do strings concatenations :
(this way, no newlines)
Else, you can have a string that’s surrounded by either single or double quotes, and put it on several lines :
(Here, again, you’ll have newlines in the resulting string)