Skip to content

Include constant in string without concatenating

In PHP, you can include a constant in a string without concatenating it by using double quotes (") instead of single quotes (') to define the string.

For example, if you have a constant defined as:

Example of defining constants in PHP

php
<?php

define("MY_CONSTANT", "Hello World");

<div class="alert alert-info flex not-prose"> Watch a course Learn object oriented PHP</div>

You can include it in a string as follows:

Example of including constant in string without concatenating in PHP

php
<?php

define("MY_CONSTANT", "Hello World");

echo sprintf("This is my constant: %s", MY_CONSTANT);

Dual-run preview — compare with live Symfony routes.