Created
December 6, 2012 01:52
-
-
Save shoyan/4221221 to your computer and use it in GitHub Desktop.
定数をリテラルの中で使う方法
This file contains 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: {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