Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is the best method to perform string concatenation in PHP?

Tags:

string

php

In php we can append strings in many ways.

            Method 1
            ----------------------------
            $sql  = "SELECT field1, ";
            $sql .= "       field2, ";
            $sql .= "       field3, ";
            $sql .= "       field4, ";
            $sql .= "FROM   table1 ";
            $sql .= "WHERE  condition1 = '".$value."' " ;

            Method 2
            ----------------------------
            $sql  = 'SELECT field1, ';
            $sql .= '       field2, ';
            $sql .= '       field3, ';
            $sql .= '       field4, ';
            $sql .= 'FROM   table1 ';
            $sql .= 'WHERE  condition1 = "'.$value.'" ' ;

            Method 3
            ----------------------------
            $sql  = 'SELECT field1,
                           field2,
                           field3,
                           field4,
                    FROM   table1
                    WHERE  condition1 = "'.$value.'" ' ;

            Method 4
            ----------------------------
            $str = <<<HEREDOC
                    SELECT field1,
                           field2,
                           field3,
                           field4,
                    FROM   table1
                    WHERE  condition1 = "$value"
            HEREDOC;

Which of the above is the best approach? Are there any other methods which will result in better performance?

like image 814
Manu Avatar asked Nov 30 '10 06:11

Manu


People also ask

How do I concatenate strings in PHP?

String Operators ¶ The first is the concatenation operator ('. '), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator (' . = '), which appends the argument on the right side to the argument on the left side.

Which of the following is used for string concatenation in PHP?

Concatenation Operator ("."): In PHP, this operator is used to combine the two string values and returns it as a new string.

What is the most efficient way to concatenate many strings together?

If you are concatenating a list of strings, then the preferred way is to use join() as it accepts a list of strings and concatenates them and is most readable in this case. If you are looking for performance, append/join is marginally faster there if you are using extremely long strings.

What are the 2 methods used for string concatenation?

There are two ways to concatenate strings in Java: By + (String concatenation) operator. By concat() method.


3 Answers

I vote for Method 3.

The less concatenation operations, the better.

Also, you should use single quotes as often as possible to avoid the PHP parser having to interpolate variables inside your strings.

like image 91
Jacob Relkin Avatar answered Oct 16 '22 21:10

Jacob Relkin


As far as I'm concerned there is no 'best method'.
Rule of thumb is choose one you prefer and stick to it and be consistent across the project.

like image 43
Michal M Avatar answered Oct 16 '22 21:10

Michal M


creating the query itself doesn't take the smallest amount of memory or cpu(it does but in such small nr that it doesnt matter) from your computer but running it, is what you should be concerned about, make sure you have a proper index and so on

like image 35
Breezer Avatar answered Oct 16 '22 20:10

Breezer