

Remember, that you can do much more than just inserting variable values in strings, for example, specify number format (hex, decimal, octal), number of decimals, padding and many more.Īt Droptica, we have been providing PHP development services for over 10 years. You can use it, but for shorter strings, as it becomes pretty hard to use this solution after 4-5 variables. The most flexible and powerful option is sprintf, but also the slowest one.Both heredoc and nowdoc syntaxes are very useful when we want to define a multiline string. Performance is equal to #2. Heredoc acts like a double quoted string, which might be also the option for you.Also, it is one of the fastest approaches, especially in hard scenarios. As a result, it is easier to both write and read such strings. Double quoted strings are more elegant because you don't have to break up your string every time you need to insert a variable (like you must do with single quoted strings).The same happens with performance, the string with more concatenations is less efficient against the #2 and #3. Otherwise, you will get the maintenance mess, increasing with the complexity of strings, because you have to deal with a huge number of quotes, concatenation and extra spaces. Use single quotes by default unless you need to use variables within the string.Here are the recommendations for each approach: We find an in-string option the most usable method for combining the strings and variables. But general advice is to use each method for a certain scenario. Today, the argument that one performs better than the other doesn’t hold any water, unless you start combining the string with variables, then the double-quotes method is definitely a winner here. PHP will not use additional processing to interpret what is inside the single quote, while inside double quotes PHP has to parse and check if there are any variables. The complicated is Knock knock, "$lang" has you.The simple case is Knock knock, $lang has you.', $size, $text1, $text2, $display) Performance comparison In this case you can try playing with ordered placeholders like %1$s, %2$s, additionally that placeholders can be repeated without adding more arguments in the code. Right after the line-height we need to figure out that it is the third parameter and list it as an argument between $size and $text1. In the above example, if we want to add a new variable display: %s Otherwise, you will have a maintenance nightmare with 10+ parameters, trying to add new %s, finding the position in the parameter list and add it to the sprintf call. Passing a multi-line string doesn’t look great, unless you want to assign it to some variable first.Īlso, remember to keep the number of variable placeholders low. It is very easy to make a mistake: display:'.

While for a single quoted case we need to find the right place before adding more concatenations and quotes. With double quotes, the PHP compiler will expand every escaped variable inside a string literal. $var #2 Double quotes and in-string variable That means you need to use concatenation. Unlike the other options, variables in a single-quoted string won’t be expanded when they occur. Theory #1 Single quotes with concatenationĪ single-quoted string does not have variables within it interpreted. We’re going to focus on each option, its strong and weak points.

The most common approaches to adapt a string are to use: It’s important to have a good understanding of how you can combine string literals with any variable. Sometimes, these strings are simple and we can use them as-is, but more often than not we need them to be dynamic, combined with different variables. In PHP we have several options for specifying strings, but most of us are only familiar with defining string literals using quotes.
