1

Possible Duplicate:
Include constant in string without concatenating

Hi

I saw you can call call variables inside strings like this: $X = "bla bla $variable"; Can I also do this with constants, and if I can, what's the synthax?

Community
  • 1
  • 1
Alex
  • 66,732
  • 177
  • 439
  • 641

4 Answers4

7

You can't call it inside strings, so you have to type $X = "bla bla " . constant.

If you really really wanted to call it from inside a string, then you'd have to use the get_defined_constants function :

$consts = get_defined_constants();
echo "bla bla {$consts['constant']}";

But really, there's no point ;-)

Vincent Savard
  • 34,979
  • 10
  • 68
  • 73
2

Not possible to do that by default. It might be possible to write a function to parse the string, but why would you want to do that :P

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
1

Yes.

$X = "bla bla" . CONSTANT_NAME;

Vic
  • 1,336
  • 11
  • 30
1

There is also the command interpolation trick:

$php = "htmlspecialchars";
# or = function ($s) {return $s}

$string = "bla bla {$php(constant('CNAME')} bla";
$string = "but just {$php(CONSTANT_NAME)} should also work";
mario
  • 144,265
  • 20
  • 237
  • 291