Equivalence of "+=" of javascript in php?

8

In javascript there is this: += to go concatenating values after those that have already been added.

Is there something similar in php?

What I want to do is a forEach of a array that adds the elements of the array to a string

PHP:

$numeros = array("uno ","dos ","tres ");
$numerosencadena = "";
foreach ($numeros as $valor) {
    // Aquí necesito ir concatenando los valores en $numeroscadena
    //para que queda algo así $numeroscadena = "uno dos tres "
}
unset($valor);

In javascript it would be something like that

var numeros = [];
var numeroscadena;

numeros.forEach(valor => {
    numeroscadena+= valor;
)
    
asked by Emiliano Pamont 22.08.2018 в 05:38
source

3 answers

12

In PHP the equivalent for which you ask is this: .= and it is called operator Concatenation mapping , which adds the argument from the right side to the argument on the left side. Of course, the variable must exist before using it, you must take care of that or your error log will be filled with PHP Notice: Undefined variable , although it will work the same, do not use the variable without first declaring it empty or with a desired initial value.

In the example that you put the code would be like this:

$numeros = array("uno ","dos ","tres ");
$numerosencadena = "";
foreach ($numeros as $valor) {
    // Aquí necesito ir concatenando los valores en $numeroscadena
    //para que queda algo así $numeroscadena = "uno dos tres "
    $numerosencadena .=$valor;
}

echo $numerosencadena;

Exit:

uno dos tres

Another possibility

In the specific case of arrays you can use implode which serves precisely for join elements of an array in a single string .

implode receives two parameters: the first is the value that will be pasted at the end of each value of the array, and the second is the array itself.

In this case, a line of code would be enough to do what you want. Here you indicate that you want each value of the array separated by a blank space: " " :

$conImplode = implode(" ", $numeros);
echo $conImplode;

Exit:

uno dos tres
    
answered by 22.08.2018 / 06:01
source
6

It's called concatenación , and it's with the point " . ", example:

$nombre = 'Emiliano';
$apellido = 'Pamont';

$nombre_completo = $nombre . $apellido;

echo $nombre_completo; //Retorna: Emiliano Pamont
echo 'Hola ' . $nombre //Retorna: Hola Emiliano

For what you say the foreach would be something like the following:

<?php 

$numeros = array("uno ","dos ","tres ");
$numerosencadena = "";

foreach ($numeros as $valor) {
    $numerosencadena .= $valor;
}

echo $numerosencadena; //Salida: uno dos tres
?>
    
answered by 22.08.2018 в 05:45
5

If there is a form similar to + =, it would be

$numeros = array("uno ","dos ","tres ");
$numerosencadena = "";
foreach ($numeros as $valor) {
    $numerosencadena .= $valor;
}

$numerosencadena = trim($numerosencadena);

The trim to eliminate the blank space at the end of the cycle, so it's optional.

    
answered by 22.08.2018 в 05:59