将变量添加到常量,php

Jer*_*Roy 0 php variables constants

我已经定义了一些像这样的常量:

define("TITLE_2","Yahoo");
define("TITLE_3","Google");
Run Code Online (Sandbox Code Playgroud)

接下来,我想通过一个函数获得常量.

public function title($id) {
    return TITLE_.$id;
}
Run Code Online (Sandbox Code Playgroud)

想法,我可以打电话给$ this-> title(2)来获得雅虎和$ this-> title(3)来获得谷歌.但是,它不起作用.我得到TITLE_2或TITLE_3代替雅虎或谷歌.

救命?谢谢.

Rob*_*gar 7

使用常量函数

public function title($id) {
    return constant("TITLE_$id");
}
Run Code Online (Sandbox Code Playgroud)