Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
24
Help us understand the problem. What is going on with this article?

More than 5 years have passed since last update.

@ukisoft

PHPの関数内static変数

PHPは、関数の中でstatic変数を宣言すると、その関数が初めて呼ばれた時にのみ初期化をし、それ以降は何度呼ばれても初期化処理をスキップしつつ、値を保持する機能があります。

function staticVar() {
    static $var = 0;
    $var++;
    var_dump($var);
}

staticVar(); // 1
staticVar(); // 2
staticVar(); // 3

こんな感じで。
クラス化してメソッドにしても同様みたいです。
今のところ、良い使いドコロが思いつきませんけど・・・。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
24
Help us understand the problem. What is going on with this article?