-1

I'm using PHP 7.2.6

I've tried following program :

<?php

  class beers {
    const softdrink = 'rootbeer';
  }

  $rootbeer = 'A & W';

  echo "I'd like an {${beers::softdrink}}\n"; //Output : I'd like an A & W

  echo "I'd like an {beers::softdrink}\n"; //Output : I'd like an {beers::softdrink} 

?>

In the above statement, in first echo statement value of the class constant softdrink is evaluated as the string 'rootbeer' which in turn a variable name which contains the string 'A & W'. The value 'A & W' gets printed.

In second echo statement I only want to show the value present in the class constant softdrink which is the string 'rootbeer'.

But I'm not able to do it. Please help me in this regard.

P.S. : Please don't ask me to achieve the output using string concatenation. I want to achieve the output only by using the class constant within a double quotes string.

PHPLover
  • 1
  • 51
  • 158
  • 311
  • 2
    Possible duplicate of [Include constant in string without concatenating](https://stackoverflow.com/questions/2203943/include-constant-in-string-without-concatenating) – M. Eriksson Jul 10 '18 at 11:34
  • @MagnusEriksson : My question is about class constants and the question you linked as duplicate is about normal constant, so please remove the 'Duplicate' mark on my question. – PHPLover Jul 10 '18 at 11:39
  • 1
    There's no difference in how a class constant and a "normal" constant is treated in a string, so the duplicate is very much valid. – M. Eriksson Jul 10 '18 at 11:40
  • 2
    Did you take this example from an answer in [this question](https://stackoverflow.com/questions/6333755/class-constant-in-a-string-in-php)? If so, read the accepted answer where it tells you it isn't possible. – iainn Jul 10 '18 at 11:45

3 Answers3

1

Afaik this is not possible. The whole "variable parsing" (extended curly syntax) in strings is done base on variables. Variables always start with a $ sign, so not starting with a $ does not seem to work. (i.e. Everything which is not (part of) a variable cannot be used.).

Simplified example (which wont work):

const TEST = 'A & W';
echo "I'd like an {TEST}\n";

Same for function calls (which also do not work directly)

$test = '   A & W   ';
echo "I'd like an {trim($test)}\n";

Only in "sub" curly braces the desired output can be used, but it has to be parsed as a variable again which makes it impossible (at this point).

Does work:

$AW = 'A & W';
$test = '   AW   ';
echo "I'd like an {${trim($test)}}\n";

Edit:

If you truly WANT to output a (class) constant inside a complex curly brace expression:

class beers {
    const softdrink = 'rootbeer';
}

function foobar($test) {
    $GLOBALS[$test] = $test;
    return $test;
}

echo "I'd like an {${foobar(beers::softdrink)}}\n";

This is far from what I would recommend to do!!!

Ronald Swets
  • 1,669
  • 10
  • 16
  • Does the value of class constant evaluated and used internally only when we use the code {${trim($test)}} in double curly braces? – PHPLover Jul 10 '18 at 12:08
  • It does, but then you have to use it somehow as a variable again. The only workaround would be to define an extra function and introduce uglyness beyond foobar (see updated answer) – Ronald Swets Jul 10 '18 at 12:18
  • The line {${foobar(beers::softdrink)}} should have to output 'A & W' as $rootbeer = 'A & W';(According to my code) – PHPLover Jul 10 '18 at 13:13
  • I think you wanted the output rootbeer instead of A & W, else the question would be irrelevant. ("In second echo statement I only want to show the value present in the class constant softdrink which is the string 'rootbeer'.") – Ronald Swets Jul 10 '18 at 13:18
  • Yes, I want to but my doubt is why it's not getting evaluated again as the variable of same name also exists? – PHPLover Jul 10 '18 at 13:25
  • The variable is overwritten in the global scope. To solve this problem, prefix the global. Like: function foobar($test) { $GLOBALS['myfoobarprefix' . $test] = $test; return 'myfoobarprefix' . $test; } – Ronald Swets Jul 10 '18 at 13:37
0

Do it like this -

echo "I'd like an ". beers::softdrink ."\n";
0

You can do it using printf -

printf("I'd like an %s\n", beers::softdrink);