PHP - concatenate or directly insert variables in string

前端 未结 14 2394
無奈伤痛
無奈伤痛 2020-11-22 04:45

I am wondering, What is the proper way for inserting PHP variables into a string?

This way:

echo \"Welcome \".$name.\"!\"


        
相关标签:
14条回答
  • 2020-11-22 05:13

    From the point of view of making thinks simple, readable, consistent and easy to understand (since performance doesn't matter here):

    • Using embedded vars in double quotes can lead to complex and confusing situations when you want to embed object properties, multidimentional arrays etc. That is, generally when reading embedded vars, you cannot be instantly 100% sure of the final behavior of what you are reading.

    • You frequently need add crutches such as {} and \, which IMO adds confusion and makes concatenation readability nearly equivalent, if not better.

    • As soon as you need to wrap a function call around the var, for example htmlspecialchars($var), you have to switch to concatenation.

    • AFAIK, you cannot embed constants.

    In some specific cases, "double quotes with vars embedding" can be useful, but generally speaking, I would go for concatenation (using single or double quotes when convenient)

    0 讨论(0)
  • 2020-11-22 05:13

    If you want to execute a SQL command and your variables are array members, then you should not use single quotes inside [] of array (like this: ['']); for example if you use this string as a SQL command, you get server error 500:

    $con = mysqli_connect('ServerName', 'dbUsername', 'dbPassword');
    mysqli_select_db($con, 'dbName')
    
    //'ID' is auto increment field.
    $sql = "INSERT INTO sampleTable (ID, TraceNo) VALUES ('','$sampleArray['TraceNo']')";
    mysqli_query($con, $sql)
    

    The correct string is:

    //'ID' is auto increment field.
    $sql = "INSERT INTO sampleTable (ID, TraceNo) VALUES ('','$sampleArray[TraceNo]')";
    
    0 讨论(0)
  • 2020-11-22 05:15

    Do not concatenate. It's not needed, us commas as echo can take multiple parameters

    echo "Welcome ", $name, "!";
    

    Regarding using single or double quotes the difference is negligible, you can do tests with large numbers of strings to test for yourself.

    0 讨论(0)
  • 2020-11-22 05:17

    I know this question already has a chosen answer, but I found this article that evidently shows that string interpolation works faster than concatenation. It might be helpful for those who are still in doubt.

    0 讨论(0)
  • 2020-11-22 05:21

    Between those two syntaxes, you should really choose the one you prefer :-)

    Personally, I would go with your second solution in such a case (Variable interpolation), which I find easier to both write and read.

    The result will be the same; and even if there are performance implications, those won't matter 1.


    As a sidenote, so my answer is a bit more complete: the day you'll want to do something like this:

    echo "Welcome $names!";
    

    PHP will interpret your code as if you were trying to use the $names variable -- which doesn't exist. - note that it will only work if you use "" not '' for your string.

    That day, you'll need to use {}:

    echo "Welcome {$name}s!"
    

    No need to fallback to concatenations.


    Also note that your first syntax:

    echo "Welcome ".$name."!";
    

    Could probably be optimized, avoiding concatenations, using:

    echo "Welcome ", $name, "!";
    

    (But, as I said earlier, this doesn't matter much...)


    1 - Unless you are doing hundreds of thousands of concatenations vs interpolations -- and it's probably not quite the case.

    0 讨论(0)
  • 2020-11-22 05:22

    You Should choose the first one. They have no difference except the performance the first one will be the fast in the comparison of second one.

    If the variable inside the double quote PHP take time to parse variable.

    Check out this Single quotes or double quotes for variable concatenation?

    This is another example Is there a performance benefit single quote vs double quote in php?

    I did not understand why this answer in above link get upvoted and why this answer got downvote.

    As I said same thing.

    You can look at here as well

    What is faster in PHP, single or double quotes?

    0 讨论(0)
提交回复
热议问题