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 8445459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:39:58+00:00 2026-06-10T09:39:58+00:00

I got this example when working with static scope in PHP function: function testStatic()

  • 0

I got this example when working with static scope in PHP function:

function testStatic() {
static $a;
echo "here is a first time: ".$a."<br />";
$a = 23;
static $a = 100;
echo "here is a second time: ".$a."<br />";
}

when I run this function like this

teststatic(); echo "<hr />";
teststatic();

It output result below:

here is a: 100
here is a: 23


here is a: 23
here is a: 23

But I expect it to be the following:

here is a: null
here is a: 100


here is a: 100
here is a: 100

I have been thinking hours try to explain why I received the result above but really failed.
Could you tell me why we have the result above? Thank you!

  • 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-06-10T09:40:00+00:00Added an answer on June 10, 2026 at 9:40 am

    It’s hard to believe that the output you indicate is really the output of that code. Yet the problem is clear here:

    A static variable is bound to a function or class and declared via the static keyword. You are re-declaring $a; this should (I guess) raise a warning.

    As static properties are part of the function’s or class’ definition, (apparently) the last occurrence of it will be “attached” to the function/class in question.

    Only the first time the function is called $a is assigned the value of 23. After the first echo statement.

    This is a 2-step process, first the parser will read the function’s definition, including static properties. After that the code will run, and the properties are mutated.

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

Sidebar

Related Questions

thank you for looking i got this example from my book but i cant
GridView CurrentGrid =Grd_UserList; Grd_UserList.Columns.Clear(); ı have got this example, ı use Columns.Clear(); fonc. with
For example I got this 2 table dbo.fc_states StateId Name 6316 Alberta 6317 British
I've got this piece of code to toggle a side panel: $(.example_wrapper_panel_link).click(function() { $(.example_wrapper).addClass('example_wrapper_active');
I've placed panel from this example http://docs.sencha.com/ext-js/4-0/#!/example/dd/dnd_grid_to_grid.html to Ext.window.Window and got an issue: proxy
I was trying to run a request factory example, but, I got this error
I got the example in this link . I can't understand the addmove ,
For example, i got a method like this: - (void)addToRecents:(NSDictionary *)photo { NSUserDefaults *defaults
Lets say I got this for example (from java obfuscation) with a highly overflowed
I got this cookie returned from web-server in Set-Cookie header: PHPSESSID=462rutd8g0sbg9sinh3hqa2ol6; path=/,wordpress_test_cookie=WP+Cookie+check; path=/,wordpress_ca07030412dd7fcd4dbeaae0bd9f5df9=testemail@example.com; expires=Sun,

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.