LoginSignup
5
6

More than 5 years have passed since last update.

if文の中に書くfunction定義

Posted at

PHPではif文のブロックの中で関数定義したときは特別な意味を持つ。

例えば、以下の例ではhello関数の定義が二カ所あるがエラーにはならない。

<?php
$a = true;

if ($a) {
    function hello() { echo "hello\n"; }
}
else {
    function hello() { echo "HELLO\n"; }
}

hello();

実行結果

hello

よく見ると、cfunctionなる記述もある。内部的にも特別に扱われているようだ。

ただ、私はこの機能を実際に活用しているコードをまだ見たことがない。

5
6
0

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
5
6