Concatenate strings in PHP

Joining texts in PHP is essential for any programmer and is therefore part of the basics of programming in this web programming language.

Join texts or strings in programming is known as CONCATENATE, and it is very simple. For this task we will always use the operator spot.

Texts can be concatenated in any programming language, it is even possible to concatenate in SQL. In this article you will learn how to concatenate with PHP.

In this article I propose 2 points to understand well how to concatenate in PHP:

  1. How to concatenate PHP, examples and simple explanation.
  2. Concatenation errors.

Content

  • How to concatenate in pHP
    • Example of joining texts in PHP
  • Ways to concatenate in PHP
    • Example of concatenating multiple elements
    • Example of concatenating numbers and variables
    • Concatenate value to a variable
  • Concatenation errors

How to concatenate in pHP

As well as to add in PHP we use the operator “+” to concatenate or join texts in PHP we use the operator spot.

With the dot symbol You will be able to join as many text strings, numbers or characters as you want, always placing it between the values ​​or variables to be joined.

Example of joining texts in PHP

In the following example you can see how to join two php variables in one with operator CONCAT:

<?php
$var1 = 'texto parte 1';
$var2 = 'texto parte 2';
$texto_completo = $var1 . $var2;
echo $texto_completo;
?>

The result of running this code would be:

texto parte 1texto parte 2

Ways to concatenate in PHP

Really there is only one way to concatenate texts in PHPbut we can find different types of unions or factors intervening in the concatenation: texts, variables, numbers, etc.

We will always solve it in the same way and that is that PHP will transform the numbers or values ​​to join to “type” text.

Example of concatenating multiple elements

In the following example I want to show you that there is no limit of variables or values ​​to concatenate:

<?php
$var1 = ' aprende a concatenar textos ';
$var2 = ' ah, y bienvenido a SRCodigofuente';
$concatenacion = $var1 . ' - ' . 'visitante de la web ' . $var2;
echo $concatenacion;
?>

The result of the previous example that joins variables and text strings would be:

 aprende a concatenar textos - visitante de la web ah, y bienvenido a SRCodigofuente

Example of concatenating numbers and variables

The following example is a little more complex and will allow you to understand even better the concatenation possibilities:

<?php
$var1 = 'No hay límites en las concatenaciones';
$var = 2;
$resultado = $var1 . ' observa como concateno ' . $var . ' números: ' . 3 . 4;
echo $resultado;
?>

The result of this exercise would be:

No hay límites en las concatenaciones observa como concateno 2 números: 34

Concatenate value to a variable

There is an alternative way of concatenating: when we want to add a text to a variable quickly:

<?php
$texto = 'Bienvenido a este tutorial';
$texto2 = ' para aprender a unir textos en PHP';
$texto = $texto . $texto2;

//esto de arriba es equivalente a lo siguiente

$texto = 'Bienvenido a este tutorial';
$texto2 = ' para aprender a unir textos en PHP';
$texto .= $texto2 ;
?>

The 2 operations above produce the same result in the $text variable, but while in the first I have concatenated the value of the $text variable itself to the right of the “=”, in the second I have added the content of “$text2” with the operation “.=”;

Concatenation errors

Last but not least, I would like to tell you about the errors that can occur when concatenating PHP elements.

Among the most common errors are:

  • Forget to include a “.” between the elements to concatenate.
  • Attempting to concatenate Arrays.
  • Try to concatenate Objects.

Leave a Reply