You can also approach the problem by assigning the value of the constant to a variable.

Personally I do it that way because if you have lots of constants in your string then your sprintf() call can be quite messy. It's also then harder to scan through the string and see what is doing what. Plus, by assigning the variables individually, you can see what is taking on what value.

An example would be:

$const = CONST;
$variable = VARIABLE;
$foo = (new Foo)->setFooProperty(12)->getFooProperty();
$bar = (123 - 456) * 10;
$ten = 1 + 2 + 1 + (5 - 4);
<<<EOD
Lorem ipsum dolor sit amet, **$variable** adipiscing elit.
Duis gravida aliquet dolor quis gravida.
Nullam viverra urna a velit laoreet, et ultrices purus condimentum.
Ut risus tortor, facilisis sed porta eget, semper a augue.
Sed adipiscing erat non sapien commodo volutpat.
Vestibulum nec lectus sed elit dictum accumsan vel adipiscing libero.
**$const** vehicula molestie sapien.
Ut fermentum quis risus ut pellentesque.
Proin in dignissim erat, eget molestie lorem. Mauris pretium aliquam eleifend.
**$foo** vitae sagittis dolor, quis sollicitudin leo.
Etiam congue odio sit amet sodales aliquet.
Etiam elementum auctor tellus, quis pharetra leo congue at. Maecenas sit amet ultricies neque.
Nulla luctus enim libero, eget elementum tellus suscipit eu.
Suspendisse tincidunt arcu at arcu molestie, a consequat velit elementum.
Ut et libero purus. Sed et magna vel elit luctus rhoncus.
Praesent dapibus consectetur tortor, vel **$bar** mauris ultrices id.
Mauris pulvinar nulla vitae ligula iaculis ornare.
Praesent posuere scelerisque ligula, id tincidunt metus sodales congue.
Curabitur lectus urna, porta sed molestie ut, mollis vitae libero.
Vivamus vulputate congue **$ten**.
EOD;
Answer from Lukey on Stack Overflow
Top answer
1 of 9
66

You can also approach the problem by assigning the value of the constant to a variable.

Personally I do it that way because if you have lots of constants in your string then your sprintf() call can be quite messy. It's also then harder to scan through the string and see what is doing what. Plus, by assigning the variables individually, you can see what is taking on what value.

An example would be:

$const = CONST;
$variable = VARIABLE;
$foo = (new Foo)->setFooProperty(12)->getFooProperty();
$bar = (123 - 456) * 10;
$ten = 1 + 2 + 1 + (5 - 4);
<<<EOD
Lorem ipsum dolor sit amet, **$variable** adipiscing elit.
Duis gravida aliquet dolor quis gravida.
Nullam viverra urna a velit laoreet, et ultrices purus condimentum.
Ut risus tortor, facilisis sed porta eget, semper a augue.
Sed adipiscing erat non sapien commodo volutpat.
Vestibulum nec lectus sed elit dictum accumsan vel adipiscing libero.
**$const** vehicula molestie sapien.
Ut fermentum quis risus ut pellentesque.
Proin in dignissim erat, eget molestie lorem. Mauris pretium aliquam eleifend.
**$foo** vitae sagittis dolor, quis sollicitudin leo.
Etiam congue odio sit amet sodales aliquet.
Etiam elementum auctor tellus, quis pharetra leo congue at. Maecenas sit amet ultricies neque.
Nulla luctus enim libero, eget elementum tellus suscipit eu.
Suspendisse tincidunt arcu at arcu molestie, a consequat velit elementum.
Ut et libero purus. Sed et magna vel elit luctus rhoncus.
Praesent dapibus consectetur tortor, vel **$bar** mauris ultrices id.
Mauris pulvinar nulla vitae ligula iaculis ornare.
Praesent posuere scelerisque ligula, id tincidunt metus sodales congue.
Curabitur lectus urna, porta sed molestie ut, mollis vitae libero.
Vivamus vulputate congue **$ten**.
EOD;
2 of 9
49

Use sprintf()

define('my_const', 100);
$string = <<< heredoc
      <p>The value of my_const is %s.</p>
heredoc;

$string = sprintf($string, my_const);
🌐
Reddit
reddit.com › r/phphelp › how do you use define() constants with heredoc syntax?
r/PHPhelp on Reddit: How do you use define() constants with heredoc syntax?
February 18, 2022 -

I have some define() constants that I want to echo inside some heredoc syntax but it's not working.

E.g.

define("TITLE", "HelloWorld!");

echo <<<END
	The title of this page is TITLE
END;

The above example will just print the string literal "TITLE" and not the defined value "HelloWorld". The only work around I can do is just store the constant in a normal variable and use it that way.

Is this a bug or am I doing something wrong.

Thank you.

🌐
PHPBuilder
board.phpbuilder.com › d › 10225427-constants-and-heredoc-syntax
Constants and Heredoc Syntax - PHPBuilder Forums
January 20, 2003 - Hi there Is there a simple way to include constants inside a string when it's being given a value in Heredoc syntax, without assigning the constant's value...
🌐
PHP
php.net › manual › en › language.types.string.php
PHP: Strings - Manual
It is also possible to use the Heredoc syntax to pass data to function arguments: ... It's possible to initialize static variables and class properties/constants using the Heredoc syntax:
🌐
PHP Tutorial
phptutorial.net › home › php tutorial › php heredoc
PHP Heredoc and Nowdoc Strings - PHP Tutorial
April 5, 2025 - Heredoc strings are like double-quoted strings without escaping. Nowdoc strings are like single-quoted strings without escaping. Was this helpful? Yes No · Thank you for your feedback! Search · × · What is PHP · Install PHP · PHP Hello World · Syntax · Variables · Constants ·
🌐
codestudy
codestudy.net › blog › interpolate-a-constant-not-variable-into-heredoc
How to Interpolate a Constant (Not Variable) into a Heredoc in PHP: Solving the Braces Issue — codestudy.net
However, **constants**—fixed values defined with `define()` or `const`—present a unique challenge: PHP does not natively interpolate constants into heredocs, and using curly braces (a common workaround for variables) often leads to errors ...
🌐
Tutorialspoint
tutorialspoint.com › php › php_heredoc_and_nowdoc.htm
PHP - Heredoc & Nowdoc
PHP provides two alternatives for declaring single or double quoted strings in the form of heredoc and newdoc syntax.
Find elsewhere
🌐
PHP
durak.org › sean › pubs › software › php-7.0.1 › language.types.string.html
Strings - PHP 7.0.1 Documentation
It is also possible to use the Heredoc syntax to pass data to function arguments: ... As of PHP 5.3.0, it's possible to initialize static variables and class properties/constants using the Heredoc syntax:
🌐
Zend
php-legacy-docs.zend.com › manual › php5 › en › language.types.string
Strings
It is also possible to use the Heredoc syntax to pass data to function arguments: ... As of PHP 5.3.0, it's possible to initialize static variables and class properties/constants using the Heredoc syntax:
🌐
O'Reilly
oreilly.com › library › view › programming-php-3rd › 9781449361068 › ch04s01.html
Quoting String Constants - Programming PHP, 3rd Edition [Book]
February 15, 2013 - The simpler of the two ways is to put the variable name in a double-quoted string or heredoc: $who = 'Kilroy'; $where = 'here'; echo "$who was $where"; Kilroy was here · The other way is to surround the variable being interpolated with curly braces. Using this syntax ensures the correct variable is interpolated. The classic use of curly braces is to disambiguate the variable name from surrounding text: $n = 12; echo "You are the {$n}th person"; You are the 12th person · Without the curly braces, PHP would try to print the value of the $nth variable.
Authors: Rasmus LerdorfKevin TatroePeter MacIntyre
Published: 2013
Pages: 538
🌐
Hacking with PHP
hackingwithphp.com › 2 › 6 › 3 › heredoc
Heredoc – Hacking with PHP - Practical PHP
In order to allow people to easily write large amounts of text from within PHP, but without the need to constantly escape things, heredoc syntax was developed. Heredoc might be a little tricky to understand at first, but it's actually a big help. Put simply, it allows you to define your own ...
🌐
Andycarter
andycarter.dev › blog › what-are-php-heredoc-nowdoc
What are PHP Heredoc & Nowdoc? – Andy Carter
January 16, 2019 - In PHP we have several options for specifying a string value. The two most common approaches are to use either single or double quotes. We also have heredoc and nowdoc syntaxes which are useful when we want to define a multiline string.
🌐
PHP
bugs.php.net › bug.php
PHP :: Bug #74044 :: Parse error when using $this::CONSTANT in Heredoc
go to bug id or search bugs for · Before you report a bug, please make sure you have completed the following steps:
🌐
O'Reilly
oreilly.com › library › view › php-in-a › 0596100671 › ch04s04.html
4.4. Heredoc - PHP in a Nutshell [Book]
October 13, 2005 - <?php $mystring = <<<EOT This is some PHP text. It is completely free I can use "double quotes" and 'single quotes', plus $variables too, which will be properly converted to their values, you can even type EOT, as long as it is not alone on a line, like this: EOT; ?> There are several things to note about heredoc and the example above:
Author: Paul Hudson
Published: 2005
Pages: 372
🌐
Linux Hint
linuxhint.com › use-heredoc-php
Linux Hint – Linux Hint
Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
🌐
Stack Overflow
stackoverflow.com › questions › 78459052 › why-are-class-constants-not-allowed-in-heredoc
php - Why are class constants not allowed in heredoc? - Stack Overflow
I want to use a class constant in heredoc which is used in an annotation, but that gives me an error. class Example { #[MyAnnotation(schema:<<<YAML type: {${MyOtherClass::A_CONSTANT}} YAML)] function myFunction() {} } Result: PHP Fatal error: Constant expression contains invalid operations