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.
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..
You could do:
define( 'FOO', 'bar' );
$constants = get_defined_constants(true); // the true argument categorizes the constants
$constants = $constants[ 'user' ]; // this gets only user-defined constants
echo "Hello, my name is {$constants['FOO']}";
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;
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);
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";