Variable Interpolation in PHP Heredocs: The <<< Syntax Rules
A common stumbling block for developers moving from simple string concatenation to multi-line strings is the behavior of heredocs regarding variables. If you attempt to output dynamic data inside a heredoc without adjusting the syntax, your variables will render as literal text rather than their evaluated values. This happens because PHP treats heredoc strings differently based on whether they are quoted or unquoted.
In your specific case, the code fails because the heredoc delimiter is defined with single quotes: <<<'EX'. When you use single quotes in the opening tag following <<<, PHP enters a "literal" mode. In this mode, the parser treats everything between the opening and closing delimiters as plain text. It ignores variables like $data['game_name'] and special characters like < or > (HTML entities), outputting them exactly as written. To get variable interpolation to work, you must remove the single quote from the delimiter tag.
Changing the syntax to <<<"EX" or simply <<<EX tells PHP to enter "double-quoted" mode. In this mode, PHP parses the content for variables and escape sequences just as it does inside standard double-quoted strings. This allows $data['game_name'] to be resolved correctly against the $data array fetched from your SQL query.
Here is how the corrected syntax looks in context:
$response = $bdd->query('SELECT * FROM video_game');
while ($data = $response->fetch()) {
echo <<<EX
<p>Game: {$data['game_name']}</p>
<p>The owner of the game is {$data['game_owner']}</p>
EX;
}
Notice two critical changes required to make this work. First, the delimiter tag <<<'EX' has been changed to <<<EX. Second, the variables inside the string have been wrapped in curly braces {}. While PHP usually handles simple variable names without braces (e.g., $name), complex array access like $data['game_name'] often requires the braces to distinguish the end of the variable name from the start of the string content. Without them, PHP might interpret '$data' as part of the string literal before hitting the bracket operator.
Trade-offs and Potential Pitfalls
While switching to double-quoted mode solves the interpolation issue, it introduces specific constraints you must manage. In double-quoted heredocs (or standard strings), backslashes behave differently than in single-quoted literals. If your data contains characters that need escaping, such as a forward slash (/) or an ampersand (&), they might be interpreted as escape sequences.
For instance, if $data['game_name'] contains the string C:/Program Files, PHP will interpret \P as an escaped character and output C:Program Files. This is rarely desirable when displaying file paths or URLs. To prevent this, you must use double backslashes (\\) inside the heredoc content:
echo <<<EX
Path: {$data['path'] // If path contains a slash, write it as \/ }
EX;
Additionally, there is a significant performance consideration when dealing with very large blocks of static text. If you are outputting thousands of lines of HTML where only 5% of the content is dynamic variables, using a heredoc can be less efficient than concatenating strings or using sprintf. Heredocs require PHP to parse the entire block as a string expression every time it executes, which adds overhead compared to simple echo statements.
However, for readability and maintainability, especially when generating complex HTML structures like tables or forms, the slight performance cost is usually negligible. The primary trade-off remains the strict requirement for proper escaping of backslashes if your dynamic data contains forward slashes.
If you are working within a Laravel application, you might consider using Blade templating engine syntax instead. Blade offers its own interpolation syntax {{ $variable }} and handles escaping automatically, which can reduce the mental load of managing quotes and braces in raw PHP heredocs. While this specific scenario uses raw PDO queries, understanding the underlying mechanics of string interpolation remains vital when building custom views or interacting with legacy codebases where Laravel's templating might not be available.