Echo string with multiple variables

Echo string with multiple variables

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Evaluating PHP String Concatenation Patterns for Security and Performance

When displaying user data from a database row, developers often face a choice between three common approaches to constructing an HTML string. While all three methods below produce visually identical output in a standard context, they differ significantly in how they handle variable expansion, whitespace management, and security implications regarding Cross-Site Scripting (XSS).

//one
echo "$row->first_name $row->last_name <br />";

//two
echo $row->first_name . ' ' . $row->last_name .'&lt;br /&gt;';

//three
echo $row->first_name;
echo $row->last_name;
echo '<br />';

The first approach relies on double-quoted string interpolation. This is the most concise syntax, but it introduces a subtle security vulnerability if not handled with extreme care. In PHP, variables inside double quotes are expanded immediately. If $row->first_name contains a string like O'<script>alert(1)</script>, that script tag will be rendered as executable code by the browser unless explicitly escaped. While modern frameworks often sanitize output automatically, relying on implicit escaping within interpolated strings is risky when dealing with raw user input or untrusted data sources.

The second pattern uses single quotes for static parts and double dots (.) to concatenate variables. This approach forces PHP to treat the content inside single quotes as literal text until it encounters a concatenation operator. Crucially, this allows you to escape HTML entities directly within the string literals, such as using &lt;br /&gt; instead of <br />. By keeping the static HTML structure in single quotes and only injecting variables, you reduce the risk of variable values interfering with the intended HTML flow. However, this method can become verbose when handling multiple fields or complex conditions.

The third method chains separate echo statements. While functionally correct, it is generally discouraged in production code due to performance considerations and maintainability. Every call to echo requires a function lookup and a context switch within the Zend Engine. When dealing with large datasets or high-traffic applications, thousands of micro-optimizations like this accumulate into measurable latency. Furthermore, separating the output into multiple statements makes it harder to manage conditional logic; for instance, if you need to add a space only between names under certain conditions, you must nest those checks inside every individual echo statement rather than controlling them within a single string definition.

From a security standpoint, the most robust strategy involves combining the readability of concatenation with explicit escaping functions like htmlspecialchars(). Regardless of whether you choose interpolation or concatenation, the underlying data should be sanitized before being injected into HTML contexts. If you are working within a Laravel project, the framework's Blade templating engine abstracts these low-level concatenation decisions away, rendering variables directly and automatically applying XSS protection by default when using double braces {{ }}.

If you must stick to raw PHP for dynamic logic or API responses, the second approach—using dot concatenation with escaped HTML literals—is often preferred. It offers a clear distinction between data (variables) and structure (strings), making it easier to audit code for injection vulnerabilities. Additionally, using single quotes for static strings prevents unintended variable expansion that could occur if a variable name was accidentally constructed as part of the string literal in more complex scenarios.

Ultimately, the "correct" choice depends on your specific constraints regarding performance profiling and team preferences. However, prioritizing explicit escaping over syntactic convenience is the non-negotiable rule when handling user-generated content.

Our network