Created
June 29, 2011 13:45
-
-
Save nissuk/1053862 to your computer and use it in GitHub Desktop.
PHP: 変数展開時に定数を展開する単純な例
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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