86
75

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【PHP】文字列の中で定数を展開する

Posted at

文字列中に変数を混ぜてダブルクォートで囲むと変数を展開することが出来る。
定数はこれができない。

php
$hensu = "へんすう";
define("TEISU","ていすう");
echo "変数を展開→{$hensu}←変数を展開";//実行結果:変数を展開→へんすう←変数を展開
echo "定数を展開→{TEISU}←定数を展開"; //実行結果:定数を展開→{TEISU}←定数を展開

展開用のラムダを作ってしまおう。

php
$_ = function($s){return $s;};//展開用
echo "定数を展開→{$_(TEISU)}←定数を展開"; //実行結果:定数を展開→ていすう←定数を展開

定数展開できないためにヒアドキュメントをあきらめて、
.=でクエリをクチャクチャ連結してるコードが多すぎ。
ダサい。

86
75
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
86
75

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?