Skip to Content
author's profile photo Former Member
Former Member

PHP string concatenation

String concatenation is the string manipulation method when you join 2 or more strings together.

In PHP it is a quite easy task. You can use the concatenation operator which is the u2018.u2019 (dot). You can join 2 or more strings into one as follows:

$str1 = 'This';

$str2 = 'is a';

$str3 = 'string';

$full = $str1.' '.$str2.' '.$str3;

echo $full; // This is a string

Besides this you can use the operator to append a string to an existing one like this:

$str = 'Main string';

$str .= ' plus another string';

echo $str; // Main string plus another string

If you concatenate a string with a number, the number will (automatically) be converted into a string value, so the output will be u201Cstringu201D:

$num = 100;

$str = $num.' is a number';

echo $str; // 100 is a number

__________________

Trade Show Displays is the retail division of Quality One Engineering.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

1 Answer

  • author's profile photo Former Member
    Former Member
    Posted on Oct 28, 2010 at 06:31 AM

    Doesn't PHP re-concatenate with every '.' operator? Using double quotes to concatenate once would be faster than using the '.' operator multiple times to form one string. Then again I guess in some or many cases it may not matter.

    Add a comment
    10|10000 characters needed characters exceeded

    • Hi skyvia and Circuitbomb welcome to the php forum.

      Thanks for enlightening us with your summary of concatenation which is the most basic function in php and to be honest who care which is faster. If you really care find Sarah Golemon's blog and find the entry on what is happening deep inside php in concatenation. Having said that there are far easier ways to slow down php that do concatenation incorrectly.

      You could read from a database for example. That is bound to be an order of magnitude slower than concatenating strings.

      PHP is fun for a lot of reasons.

      I hope you guys both enjoy SAP and PHP in good measure and contribute the interesting things that you are learning on the forums.

      Cheers,

      Nigel

      Edited by: Nigel James on Oct 28, 2010 12:56 PM

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.