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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:15:42+00:00 2026-05-25T19:15:42+00:00

For some reason my static variable resets and I am not sure why it

  • 0

For some reason my static variable resets and I am not sure why it resets.

public static $_pwdAdjId = 0; //static variable

function ...() {
// Piece of code that manipulates the static variable
if(isset($_POST['adj'])) {
    self::$_pwdAdjId = $_POST['adj'];
} else if(!isset($_POST['adj']) && $_GET['ajax'] !== 'assignTable') {
    self::$_pwdAdjId = Yii::app()->user->getId();
}
$adj = self::$_pwdAdjId;
...
}

When the function is first called, this will be called:

self::$_pwdAdjId = Yii::app()->user->getId();

and saves the value into $adj and it works fine.

When the user changes the page the value that is stored into $adj is 0 – the initial value of the static variable. I am not sure why it is reseting to its initial value instead of the new value stored.

Before First Call:

self::$_pwdAdjId == 0

First Call:

self::$_pwdAdjId == 7

$adj == 7

Page (ajax call): Second Call:

self::$_pwdAdjId == 0 // supposed to be 7

Am I missing something? Does the fact that it’s called via ajax the second time affect its static-ness?

Thanks.

  • 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-25T19:15:43+00:00Added an answer on May 25, 2026 at 7:15 pm

    When the user changes the page, it’s a totally different HTTP request and your program runs again from scratch. static does not do what you think it does. You can achieve the result you want using session variables.

    Since it looks like you are using Yii framework, you might want to use its own wrapper over PHP sessions instead of rolling your own (by the way, it also has wrappers for getting HTTP request parameters).

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

Sidebar

Related Questions

For some reason I never see this done. Is there a reason why not?
For some reason, Section 1 works but Section 2 does not. When run in
For some reason the Windows command prompt is special in that you have to
one of my favorite annoyance when coding in C++ is declaring some static variable
I was playing with some code to make a "closure like" construct ( not
Okay, so here is my code: public static void playAgain(Scanner in){ System.out.print(Play again?: );
I have a gridview that I databind dynamically in my codebehind. For some reason,
I'm working in PHP. I have a function (F1) that gets called a variable
I encountered a problem when running some old code that was handed down to
I'm trying to define a static variable and methods in a module that will

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.