6

I'm fairly new to php. I have a very long string, and I don't want to have newlines in it. In python, I would accomplish this by doing the following:

new_string = ("string extending to edge of screen......................................."
    + "string extending to edge of screen..............................................."
    + "string extending to edge of screen..............................................."
    )

Is there anything like this that I can do in PHP?

3
  • 4
    a period concatenates strings in php.... $x = "str"."ing"; Commented Nov 4, 2013 at 22:06
  • 1
    not to mention variables need a $ before them, and a ; at the end Commented Nov 4, 2013 at 22:08
  • 1
    in php aslo this would work: $string = "some_string [new line] some_string [new_line] some_string" without adding (.) operatore Commented Nov 4, 2013 at 22:09

2 Answers 2

10

You can use this format:

$string="some text...some text...some text...some text..."
."some text...some text...some text...some text...some text...";

Where you simply use the concat . operator across many lines - PHP doesn't mind new lines - as long as each statement ends with a ;.

Or

$string="some text...some text...some text...some text...";
$string.="some text...some text...some text...some text...";

Where each statement is ended with a ; but this time we use a .= operator which is the same as typing:

$string="some text...some text...some text...some text...";
$string=$string."some text...some text...some text...some text...";
Sign up to request clarification or add additional context in comments.

1 Comment

Isn't there a better way? Something like $string = { } so we can easily mark a whole block as string?
1

Use the . operator:

$concatened = 'string1' . 'string2';

You can spread this across multiple lines and use it together with the assingment operator :

$str  = 'string1';
$str .= 'string2';

...

An alternative is to use join(). join() allows to concat and array with strings using a delimiter:

$str = join('', array(
    'string1',
    'string2'
));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.