Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7194133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:18:36+00:00 2026-05-28T20:18:36+00:00

I started to learn OO a few days back, I’m quite OK with procedural

  • 0

I started to learn OO a few days back, I’m quite OK with procedural coding but obviously that is not enough and I want to become a well versed coder with a lot of experience and knowledge, so first thing to learn completely must be OO followed by the right design patterns I think.

Anyhow, I have one thing where I’m stuck which I don’t quite follow…

Static variables… I understand that a static variable doesn’t lose it’s value even if the containing function is finished executing, and will keep it’s value if the same function is executed again, and again etc etc.

But what I don’t understand is what exactly can you now assign to a static variable? The manual and also countless question on stackoverflow state you can not assign an expression to a static variable.

So I read the PHP manual, to find out exactly what is considered an expression? The manuals answer is (and I quote):

“In PHP, almost anything you write is an expression. The simplest yet most accurate way to define an expression is “anything that has a value”.”

“When you type “$a = 5″, you’re assigning ‘5’ into $a. ‘5’, obviously, has the value 5, or in other words ‘5’ is an expression”

http://php.net/manual/en/language.expressions.php

Now when you read about variable scope in the manual, they have exactly this example:

    function test()
    {
    static $a = 0;
    echo $a;
    $a++;
    }

So, the manual about variable scopes says static $a = 0; is fine, while the manual about expressions says $a = 5, would be an expression. Which is basically the same thing, just a 0 instead of a 5…

So I’m a bit confused now.

What exactly is an expression now and what exactly can I or can I not assign to static variables? 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:18:36+00:00Added an answer on May 28, 2026 at 8:18 pm

    You cannot initialize a static variable using a non-constant expression. You can assign anything you like to it after it is initialized.

    The difference is that static variables are initialized during the parsing phase, i.e. while PHP reads through the source code to figure out what’s what. At that stage no code is executed, PHP just reads what you want it to do. Therefore, it will not execute code to initialize the variable.

    static $foo = 'bar';
    

    'bar' is a constant value which PHP can easily assign to the variable at parse time.

    static $foo = Bar::baz();
    

    Bar::baz() is an expression that needs to run, PHP needs to locate the class, load it if necessary, run the baz() method, which may do all sorts of different stuff… The same for 5 + 3, md5('bar') or anything that requires actual computation. PHP is simply not going to do all this dynamic stuff at parse time. Therefore you cannot initialize a static variable with anything but constant values.

    At runtime, you can assign anything you like to a static variable. An often used pattern is this:

    static $foo = null;
    if ($foo === null) {
        $foo = new SomeObject;
    }
    

    This keeps the instance of SomeObject in the static variable, but you cannot initialize the variable with it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've started to learn git in the past few days and I have a
I am started to learn Lua few days ago and faced next problem. How
I have recently started to learn Objective-C and write my tests using OCUnit that
I ordered Programming Windows Fifth Edition a few days ago, and started working with
I started to learn using Instrument, but I cannot figure it out. After I
To learn ASP.net I started building a todo web app(web forms not MVC), this
Few hours ago I have started to learn java socket comunication. Now I'm trying
I've started a little side project to try to learn a few new concepts
I'm trying to learn C# from some YouTube videos, but I have a few
Now I know there are quite a few questions regarding this but in general

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.