quoting constants in php: “this is a MY_CONSTANT”

前端 未结 6 1599
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-06 09:58

I want to use a constant in PHP, but I also want to put it inside double quotes like a variable. Is this at all possible?

define(\"TESTER\", \"World!\");
ech         


        
相关标签:
6条回答
  • 2020-12-06 10:08

    Alternatively, do

    "this is " . MY_CONSTANT
    

    or

    "this is " . constant("MY_CONSTANT");
    
    0 讨论(0)
  • 2020-12-06 10:09

    I've found that when dot-concatenation of a constant is a problem, using sprintf to get my string is usually the way I want to go in the end.

    0 讨论(0)
  • 2020-12-06 10:10

    Concatenation is the way to go.

    Unless you want the hokey, nasty, inefficient, evil monkey way of:

    echo preg_replace("/TESTER/",TESTER,$original_content);
    
    0 讨论(0)
  • 2020-12-06 10:22

    I recomend you to use concatenation because:

    1. When you use a variable into a double quotes string your visibility is not good;
    2. When you use a double quotes string the php can to process slowly;
    3. You don't use a constant into a string, because don't have any delimiter to the php knows what is the constant.
    0 讨论(0)
  • 2020-12-06 10:31

    Sorry, that's not the way constants in PHP work. You can put variables in double quotes and heredocs but not constants.

    0 讨论(0)
  • 2020-12-06 10:32

    no way, unless you write your own string parsing function

    0 讨论(0)
提交回复
热议问题