Logo
/
Programming
/
PHP Course
/

Concatenation

PHP: Concatenation

Often, strings need to be assembled from several parts, for example, to join a first name and a last name, add a unit of measurement, or build text from a template. The concatenation operation, that is, gluing strings together, is used for this.

How to join strings

In PHP, strings are joined using the . (dot) operator. The + operator is used to add numbers, while for joining strings PHP has a separate binary operator that means gluing the contents together:

<?php

print_r('Dragon' . 'stone');
// => Dragonstone

Order matters. First comes the left part ('Dragon'), then the right part ('stone'). The result comes out in the order in which the operands are specified.

Here's how joining several strings works. The code:

<?php

print_r('Hello' . ', ' . 'World!');

Execution:

'Hello' . ', ' . 'World!'
└──┬──┘   └┬┘   └──┬───┘
   └────┬───┘       │
  'Hello, '    .  'World!'
     └──────┬───────┘
      'Hello, World!'

Examples:

<?php

print_r('Kings' . 'wood');     // => Kingswood
print_r('Kings' . 'road');     // => Kingsroad
// Here the outer quotes are double, because there is a single one inside
print_r("King's" . 'Landing'); // => King'sLanding

PHP lets you join strings even if they're written in different quotes. The main thing is that both parts are strings.

A space is also a character

When joining, PHP doesn't insert spaces automatically. If there should be a space between the parts, it needs to be specified manually:

<?php

// A space at the end of the first string
print_r("King's " . 'Landing'); // => King's Landing

// A space at the start of the second string
print_r("King's" . ' Landing'); // => King's Landing

The result will be the same. But if you don't add a space, the words will be glued together.

Escape sequences

In strings, you can use escape sequences, for example \n for a line break or \t for a tab. Don't forget that in PHP they only work inside double quotes. During concatenation, they behave just like any other characters:

<?php

print_r('Hello,' . "\n" . 'World!');
// =>
// Hello,
// World!

print_r('A' . "\t" . 'B');
// => A	B

Conclusion

Concatenation is the joining of strings via the . operator, and strings can be joined regardless of the type of quotes.

  • Gluing happens strictly in left-to-right order
  • Spaces aren't added automatically, you need to include them in the strings manually

Instructions

The site automatically generates links to user pages by assembling them from separate parts. Assemble the link using concatenation and print it on the screen:

https://github.com/hexlet/exercises-php

Each component of the URL is a separate string: the protocol, the domain, the owner's name, and the repository name.

Tips

  • If the editor has // BEGIN and // END, then the code should be written between these lines.

If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:

  • Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Found a bug? Have something to add? Pull requests are welcome!