Use a variable within heredoc in PHP
Your heredoc needs a little modification (because it's actually Nowdoc!):
echo <<<EX
<p>Game: {$data['game_name']}<br/>
the owner of the game is {$data['game_owner']}
</p>
EX;
Heredoc identifiers (unlike nowdoc ones) cannot be quoted.
'EX'
needs to becomeEX
.You're confusing Nowdoc with heredoc.
Complex data types in strings must be surrounded by
{}
for them to be parsed as variables. For example,$data['game_name']
should be{$data['game_name']}
.In the obsoleted PHP versions ( before PHP 7.3) the heredoc terminator must not have any preceding whitespace. From the documentation:
The closing identifier may be indented by space or tab, in which case the indentation will be stripped from all lines in the doc string. Prior to PHP 7.3.0, the closing identifier must begin in the first column of the line.
You're mixing up heredoc and nowdoc here. You want to use heredoc and not Nowdoc because you've got variables inside your string. Heredocs are "extended" double quoted strings, whereas nowdocs are more akin to a single quoted string, in that variables are not parsed in nowdoc strings, but are in heredoc.
- More on heredoc here.
- More on Nowdoc here.
Please read the documentation more carefully on these.
Outputting variable within Heredoc
<?php
$email="test@example.com";
$message = <<<EOF
<p style="font-size: 9px; font-family: Verdana, Helvetica; width: 100%; text-align:left;">Click to remove <a href="http://www.mysite.com/remove.php?email=$email">clicking here.</a></p>
EOF;
echo $message;
?>
However, from your example, I don't see the purpose of HEREDOC.
Why not just:
<p style="font-size: 9px; font-family: Verdana, Helvetica; width: 100%; text-align:left;">Click to remove <a href="http://www.mysite.com/remove.php?email=<?=$email?>">clicking here.</a></p>
Inserting variables when using HEREDOC?
Figured out I don't even need to break the EOT to enter the variable, I can just add {$player_name}
directly in to the EOT code. Who would've known I was overcomplicating it...
Calling PHP functions within HEREDOC strings
I would not use HEREDOC at all for this, personally. It just doesn't make for a good "template building" system. All your HTML is locked down in a string which has several disadvantages
- No option for WYSIWYG
- No code completion for HTML from IDEs
- Output (HTML) locked to logic files
- You end up having to use hacks like what you're trying to do now to achieve more complex templating, such as looping
Get a basic template engine, or just use PHP with includes - it's why the language has the <?php
and ?>
delimiters.
template_file.php
<html>
<head>
<title><?php echo $page_title; ?></title>
</head>
<body>
<?php echo getPageContent(); ?>
</body>
index.php
<?php
$page_title = "This is a simple demo";
function getPageContent() {
return '<p>Hello World!</p>';
}
include('template_file.php');
Variables in heredoc inside object in PHP
The answer is here in the official PHP docs:
Heredocs can not be used for initializing class properties. Since PHP
5.3, this limitation is valid only for heredocs containing variables.
Variable substitution within HEREDOC literal
You have don't need to close/open quotes for the variable. Use this code instead:
$html = <<<EOD
<div>
</br>
<img src="../tcpdf/pdffirst.png" width="500" height="800" alt="Sample Image"/>
<img src="../charts/$filename-most.png" width="500" height="250" alt="Sample Image"/>
</br>
</div>
EOD;
Related Topics
Move All Files in a Folder to Another
How to Prevent PHP Files from Being Downloaded? and What Are Some Ways Someone Can Download Them
PHP Call Class Function by String Name
Chart.Js - Getting Data from Database Using MySQL and PHP
How to Set for Specific Directory Open_Basedir
PHP Pdo with Foreach and Fetch
How to Apply a Function to an Array
PHP Dom Get Nodevalue HTML? (Without Stripping Tags)
How to Write to Error Log File in PHP
PHP Date Time Current Time Add Minutes
Disable Csrf in Laravel for Specific Route
Detecting Whether a User Is Behind a Proxy
Load Block Outside Magento, and Apply Current Template