Skip to content

Instantly share code, notes, and snippets.

@nissuk
Created June 29, 2011 13:45
Show Gist options
  • Save nissuk/1053862 to your computer and use it in GitHub Desktop.
Save nissuk/1053862 to your computer and use it in GitHub Desktop.
PHP: 変数展開時に定数を展開する単純な例
<?php
define('FOO', 1);
define('BAR', 2);
// 変数展開が「{$」で始まっている場合、関数の実行等が可能です。
$c = 'constant';
echo "FOO: {$c('FOO')}, BAR: {$c('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