Categories
combinations php string variables

How can I combine two strings together in PHP?

178

I don’t actually know how to describe what I wanted, but I’ll show you:

For example:

$data1 = "the color is";
$data2 = "red";

What should I do (or process) so $result is the combination of $data1 and $data2?

Desired result:

$result = "the color is red";

0

    119

    There are several ways to concatenate two strings together.

    Use the concatenation operator . (and .=)

    In PHP . is the concatenation operator which returns the concatenation of its right and left arguments

    $data1 = "the color is";
    $data2 = "red";
    $result = $data1 . ' ' . $data2;
    

    If you want to append a string to another string you would use the .= operator:

    $data1 = "the color is ";
    $data1 .= "red"
    

    Complex (curly) syntax / double quotes strings

    In PHP variables contained in double quoted strings are interpolated (i.e. their values are “swapped out” for the variable). This means you can place the variables in place of the strings and just put a space in between them. The curly braces make it clear where the variables are.

    $result = "{$data1} {$data2}";
    

    Note: this will also work without the braces in your case:

    $result = "$data1 $data2";
    

    You can also concatenate array values inside a string :

    $arr1 = ['val' => 'This is a'];
    $arr2 = ['val' => 'test'];
    $variable = "{$arr1['val']} {$arr2['val']}";
    

    Use sprintf() or printf()

    sprintf() allows us to format strings using powerful formatting options. It is overkill for such simple concatenation but it handy when you have a complex string and/or want to do some formatting of the data as well.

    $result = sprintf("%s %s", $data1, $data2);
    

    printf() does the same thing but will immediately display the output.

    printf("%s %s", $data1, $data2);
    // same as
    $result = sprintf("%s %s", $data1, $data2);
    echo $result;
    

    Heredoc

    Heredocs can also be used to combine variables into a string.

    $result= <<<EOT
    $data1 $data2
    EOT;
    

    Use a , with echo()

    This only works when echoing out content and not assigning to a variable. But you can use a comma to separate a list of expressions for PHP to echo out and use a string with one blank space as one of those expressions:

    echo $data1, ' ', $data2;
    

    0

      28

      Another possibility is the .= operator. I’m using it for huge SQL queries.

      $string = "the color is ";
      $string .= "red";
      
      echo $string; // Gives: the color is red
      

      3

      • “I’m using it for huge sql queries.” – note that it is a typical way of getting en.wikipedia.org/wiki/SQL_injection

        Jul 17, 2019 at 7:03


      • how exactly concatenating string this or other way will give me sql injection in my parameterized query?

        – codelame

        Sep 5, 2019 at 22:27

      • 1

        Manually building entire SQL queries (at least suggested by “for huge sql queries”) is not OK. Preparing just parameters, not entire queries should be safe from this specific security risk.

        Sep 15, 2019 at 11:53