Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 711297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:43:10+00:00 2026-05-14T04:43:10+00:00

I want to have a string variable for a PHP class, which would be

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:43:10+00:00Added an answer on May 14, 2026 at 4:43 am

    If you are using PHP >= 5.3, you could use HEREDOC syntax to declare your string :

    class MyClass {
        public $str = <<<STR
    this is
    a long
    string
    STR;
    
    }
    
    $a = new MyClass();
    var_dump($a->str);
    

    But this :

    • is only possible with PHP >= 5.3
    • and the string must not contain any variable
      • this is because the string’s value must be known at compile-time
      • which, btw, explains why the concatenation, with the ., 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 :

    class MyClass {
        public $str;
        public function __construct() {
            $this->str = <<<STR
    this is
    a long
    string
    STR;
        }
    }
    

    (same not with newlines)

    Or, here, you can do strings concatenations :

    class MyClass {
        public $str;
        public function __construct() {
            $this->str = 'this is' .
                         'a long' .
                         'string';
        }
    }
    

    (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 :

    class MyClass {
        public $str = "this is
    a long
    string";
    }
    

    (Here, again, you’ll have newlines in the resulting string)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 376k
  • Answers 376k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you are wanting to limit the callers to only… May 14, 2026 at 8:35 pm
  • Editorial Team
    Editorial Team added an answer Yes, just like you would with any other type ...… May 14, 2026 at 8:35 pm
  • Editorial Team
    Editorial Team added an answer import random def main(): wordsf = open('words.txt', 'r') words =… May 14, 2026 at 8:35 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.