어떻게 define을 heredoc 안에 사용합니까? 예를 들어 :정의하고 heredoc
define('PREFIX', '/holiday');
$body = <<<EOD
<img src="PREFIX/images/hello.png" /> // This doesn't work.
EOD;
어떻게 define을 heredoc 안에 사용합니까? 예를 들어 :정의하고 heredoc
define('PREFIX', '/holiday');
$body = <<<EOD
<img src="PREFIX/images/hello.png" /> // This doesn't work.
EOD;
이 the documentation regarding strings
DEFINE('PREFIX','/holiday');
$const = PREFIX;
echo <<<EOD
<img src="{$const}/images/hello.png" />
EOD;
, 변수 사용이 어렵다. 그래서이 방법
define('PREFIX', '/holiday');
define('SUFFIX', '/work');
define('BLABLA', '/lorem');
define('ETC', '/ipsum');
$cname = 'constant'; // if you want to use a function in heredoc, you must save function name in variable
$body = <<<EOD
<img src="{$cname('PREFIX')}/images/hello.png" />
<img src="{$cname('SUFFIX')}/images/hello.png" />
<img src="{$cname('BLABLA')}/images/hello.png" />
<img src="{$cname('ETC')}/images/hello.png" />
EOD;
나는 그것이 재미 있다고 생각했기 때문에 그것을 시도하기 위해 당신의 제안을 따랐지만, 당신의 제안이 내 마음에 들었습니다. 많은 오류가 있기 때문에 테스트되지 않았습니다. – hakre
답변을 변경했습니다. –
이것은 잘 작동합니다! –
감사를 시도! 작은 메모 추가 :'$ const/images/hello.png'도 작동합니다. – moey
이 예에서는 중괄호가 필요하지 않습니다. – wlf
또한'$ consts = get_defined_constants();'를 사용하여 모든 정의를 얻은 다음'{$ consts [ 'PREFIX']}'를 사용하여 액세스 할 수 있습니다. – PhoneixS