Skip to content

Instantly share code, notes, and snippets.

@shoyan
Created December 6, 2012 01:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shoyan/4221221 to your computer and use it in GitHub Desktop.
Save shoyan/4221221 to your computer and use it in GitHub Desktop.
定数をリテラルの中で使う方法
<?php
define('FOO', 1);
define('BAR', 2);
// 変数展開が「{$」で始まっている場合、関数の実行等が可能です。
$c = 'constant';
echo "FOO: {constant('FOO')}, BAR: {constant('BAR')}" . PHP_EOL; // => FOO: 1, BAR: 2
// 通常の関数定義、create_function()、ラムダ(無名関数)等で
// 引数をそのまま返す関数を作って変数として持っておくと便利です。
$x = create_function('$x', 'return $x;');
echo "FOO: {$x(FOO)}, BAR: {$x(BAR)}" . PHP_EOL; // => FOO: 1, BAR: 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment