8

Is there a way to evaluate a constant inside double quotes to avoid concatenation using .?

For example, I can do things like:

echo "$variable";

echo "{$array["index"]}";

echo "{$this->myProperty}";

Unfortunally echo "{MY_CONSTANT}" don't work.

So, is there a way to evaluate a constant like in the examples above, avoiding concatenation?

I know is there alternatives to code and get the same result, but I'm aiming at just the constants.

My motivation to this is to write sql statements, for example:

$sql = "SELECT * FROM {MY_JOIN} WHERE id > 100";

Where MY_JOIN constant could be something like

"
orders
INNER JOIN
users
    ON (orders.user_id = users.id)
"

or to avoid something like

$dir = DIRECTORY_SEPARATOR."folder1".DIRECTORY_SEPARATOR."folder2".DIRECTORY_SEPARATOR;

I know I can write something like $separator = DIRECTORY_SEPARATOR and code $dir = "{$separator}folder1{$separator}folder2{$separator}"; but if it is possible, I would like to do this with the constants directly.

pilsetnieks
  • 10,330
  • 12
  • 48
  • 60
GarouDan
  • 3,743
  • 9
  • 49
  • 75
  • Similar question: http://stackoverflow.com/questions/1267093/php-static-variables-in-double-quotes – Nate Apr 13 '13 at 00:32
  • Thx about the links. I agree, my question was a duplicate of that one. But I think I had explained a little better and created a motivation ^^ – GarouDan Apr 13 '13 at 00:50

2 Answers2

2

No, this is simply not possible.

However, two great workarounds for your particular examples:

sprintf('SELECT ... %s ...', MY_CONSTANT);

join(DIRECTORY_SEPARATOR, array('foo', 'bar', 'baz'));
deceze
  • 510,633
  • 85
  • 743
  • 889
  • 1
    `join` is more commonly written as `implode`, although both refer to the same function – IMSoP Apr 13 '13 at 00:38
  • I'm aware of that and I prefer the term `join`. – deceze Apr 13 '13 at 00:40
  • Fair enough I suppose. One advantage of `implode` is that it naturally goes with `explode`, which importantly is *not* the same as `split`. – IMSoP Apr 13 '13 at 00:47
  • 1
    @IMSoP `split` and `join` go naturally when you also code in some given languages (JavaScript), but I agree, I prefer the terms `implode`/`explode` so my IP goes directly to FBI and my ISP's possible terrorists list whenever I look for the PHP doc. – Fabrício Matté Apr 13 '13 at 00:57
  • But in PHP, `split` is a deprecated regex function, so the further it is from your mind the better. I agree that it's a better name, but it's in use for something else. – IMSoP Apr 13 '13 at 01:04
  • I don't even know what you guys are talking about anymore. `join` is the same as `implode` in PHP, regardless of any connections to `split` in other languages which may or may not have anything to do with PHP's `split` which has nothing to do with `join`. It's the term that makes for more fluent reading of code IMO and that's all there is to it. – deceze Apr 13 '13 at 05:34
  • @deceze My point was simply that `implode` is considered the "real" name of the PHP function, and matches `explode`. `join` is an alias, which might mislead people into using `split` as its opposite. To another PHP programmer, `implode` is likely to be the more familiar name, and they might wonder (as I briefly did) if `join` is actually a subtly different function. – IMSoP Apr 13 '13 at 12:58
  • @IMSoP You're still losing me on the `join` == `split` confusion. Most other languages *only* have `split` (e.g. `String.split`), which works like PHP's `explode`, and *only* `join` (e.g. `Array.join`), which works like PHP's `join`. If anything `explode`/`implode` is "non-standard" and confusing. But anyway, Know Your Functions. ;) – deceze Apr 13 '13 at 14:52
  • @deceze Unfortunately, we're stuck with PHP's `split` function being a deprecated version of the less common case of split-on-regex (which some languages, e.g. JS, Ruby, handle by overloading `split` because they have a primitive type for regexes). The `explode`/`implode` naming may be confusing, but the mismatched pair `explode`/`join` is surely even more so, and may tempt new PHP programmers to use the incorrect pair `split`/`join`, and not understand why `split('.', 'a.b')` behaves so unexpectedly. – IMSoP Apr 13 '13 at 15:25
  • Arguable you should not use "join" *because* it's an *alias* of implode. As per http://php.net/manual/en/aliases.php - "It is usually a bad idea to use these kind of aliases, as they may be bound to obsolescence or renaming, which will lead to unportable script. This list is provided to help those who want to upgrade their old scripts to newer syntax. " – James Jun 04 '15 at 15:54
2

Interesting, as the links suggested, we can do this with lambda functions, very interesting.

An working example:

<?php
$constant = function($cons){
   return constant($cons);
};

define('FOO', 'Hello World!');
echo "The string says {$constant('FOO')}";
GarouDan
  • 3,743
  • 9
  • 49
  • 75