Is there a way in PHP to include a constant in a string without concatenating?
define('MY_CONSTANT', 42);
echo "This is my constant: MY_CONSTANT";
Is there a way in PHP to include a constant in a string without concatenating?
define('MY_CONSTANT', 42);
echo "This is my constant: MY_CONSTANT";
No.
With Strings, there is no way for PHP to tell string data apart from constant identifiers. This goes for any of the string formats in PHP, including heredoc.
constant()
is an alternative way to get hold of a constant, but a function call can't be put into a string without concatenation either.
Yes it is (in some way ;) ):
define('FOO', 'bar');
$test_string = sprintf('This is a %s test string', FOO);
This is probably not what you were aiming for, but I think, technically this is not concatenation but a substitution and from this assumption, it includes a constant in a string without concatenating.
To use constants inside strings you can[1] use the following method:
define( 'ANIMAL', 'turtles' );
$constant = constant(...);
echo "I like {$constant('ANIMAL')}!";
[1] "can" does not mean you "should", practically I advise you actually do this:
sprintf('I like %s!', ANIMAL);
{$constant('ANIMAL')}
work?The {}
brackets inside string allow us to invoke any callable as long as it starts with $
!
I.e. "I like {constant('ANIMAL')}";
without $
will not trigger variable substitution so we have to put the callable in a variable.
By the way: the constant(...)
syntax is called first class callable syntax.
One can place any function name in a variable and call it with parameters inside a double-quoted string. Works with multiple parameters too.
$fn = 'substr';
echo "I like {$fn('turtles!', 0, -1)}";
Produces
I like turtles
$escape = fn( $string ) => htmlspecialchars( (string) $string, ENT_QUOTES, 'utf-8' );
$userText = "<script>alert('xss')</script>";
echo "You entered {$escape( $userText )}";
Produces:
You entered <script>alert('xss')</script>
class Arr
{
public static function get( $array, $key, $default = null )
{
return is_array( $array ) && array_key_exists( $key, $array )
? $array[$key]
: $default;
}
}
$fn = array( 'Arr', 'get' );
echo "I like {$fn(['turtles'], 0)}!";
Produces:
I like turtles!
Try it yourself on 3v4l.org and notice - most the callable types (except string
) would not work pre-php5.3 (at the original time this post was written) - now updated to php7+++ era
This practice is ill-advised, but the possibility is there.
define( 'FOO', 'bar');
$FOO = FOO;
$string = "I am too lazy to concatenate $FOO in my string";
define('FOO', 'bar');
$constants = create_function('$a', 'return $a;');
echo "Hello, my name is {$constants(FOO)}";
If you really want to echo constant without concatenation here is solution:
define('MY_CONST', 300);
echo 'here: ', MY_CONST, ' is a number';
note: in this example echo takes a number of parameters (look at the commas), so it isn't real concatenation
Echo behaves as a function, it takes more parameters, it is more efficient than concatenation, because it doesn't have to concatenate and then echo, it just echoes everything without the need of creating new String concatenated object :))
EDIT
Also if you consider concatenating strings, passings strings as parameters or writing whole strings with " , The , (comma version) is always fastest, next goes . (concatenation with ' single quotes) and the slowest string building method is using double quotes ", because expressions written this way have to be evaluated against declared variables and functions..
The easiest way is
define('MY_CONSTANT', 42);
$my_constant = MY_CONSTANT;
echo "This is my constant: $my_constant";
Another way using (s)printf
define('MY_CONSTANT', 42);
// Note that %d is for numeric values. Use %s when constant is a string
printf('This is my constant: %d', MY_CONSTANT);
// Or if you want to use the string.
$my_string = sprintf('This is my constant: %d', MY_CONSTANT);
echo $my_string;
As others have pointed out you can not do that. PHP has a function constant()
which cant be called directly in a string but we can easily work around this.
$constant = function($cons){
return constant($cons);
};
and a basic example on its usage:
define('FOO', 'Hello World!');
echo "The string says {$constant('FOO')}";
Here are some alternatives to the other answers, which seem to be focused mostly on the "{$}" trick. Though no guarantees are made on their speed; this is all pure syntactic sugar. For these examples, we'll assume the set of constants below were defined.
define( 'BREAD', 'bread' ); define( 'EGGS', 'eggs' ); define( 'MILK', 'milk' );
Using extract()
This one is nice because the result is identical to variables. First you create a reusable function:
function constants(){ return array_change_key_case( get_defined_constants( true )[ 'user' ] ); }
Then call it from any scope:
extract( constants() );
$s = "I need to buy $bread, $eggs, and $milk from the store.";
Here, it lowercases the constants to be easier on your fingers, but you can remove the array_change_key_case() to keep them as-is. If you already have conflicting local variable names, the constants won't override them.
Using string replacement
This one is similar to sprintf(), but uses a single replacement token and accepts an unlimited number of arguments. I'm sure there are better ways to do this, but forgive my clunkiness and try to focus on the idea behind it.
Like before, you create a reusable function:
function fill(){
$arr = func_get_args(); $s = $arr[ 0 ]; array_shift( $arr );
while( strpos( $s, '/' ) !== false ){
$s = implode( current( $arr ), explode( '/', $s, 2 ) ); next( $arr );
} return $s;
}
Then call it from any scope:
$s = fill( 'I need to buy /, /, and / from the store.', BREAD, EGGS, MILK );
You can use any replacement token you want, like a % or #. I used the slash here since it's a bit easier to type.
Late to the party but here's my solution in case you don't like (s)printf :
// Can of course be replaced with the const syntax
define("FOO", "bar");
$replace_pairs = ["%myConstant" => FOO];
// Output : "My constant : bar"
echo strtr("My constant : %myConstant", $replace_pairs);
It is fun that you can use keyword 'const' as a name for your function to prevent namespace littering:
define("FOO","foo");
${'const'} = function($a){return $a;};
echo "{$const(FOO)}"; // Prints "foo"
echo const(FOO); // Parse error: syntax error, unexpected T_CONST
You can also use $GLOBALS to propagate 'const' function all over the code:
$GLOBALS['const'] = function($a){return $a;};
Unsure is it safe for future using. And what is worse - it's still looks ugly.
I need it pretty frequently, mostly while writing scripts, to print new line characters.
When running the script from browser, the newline should be <br>
and while executing it from terminal it should be PHP_EOL
So, I do something like:
$isCLI = ( php_sapi_name() == 'cli' );
$eol = $isCLI ? PHP_EOL : '<br>';
echo "${eol} This is going to be printed in a new line";
This is an old question but for those searching here is another way to do it
<?php
define( 'MY_CONSTANT', 'world' );
echo "Hello {${$constant = 'constant'}('MY_CONSTANT')}";
I believe this answers the OP's original question. The only thing is the globals index key seems to only work as lower case.
define('DB_USER','root');
echo "DB_USER=$GLOBALS[db_user]";
Output:
DB_USER=root