PHP变量问题

here is my code

echo ("<br/>");
if ($APIkbpersec < 30) {
    global $SpeedTest;
    echo ("Slow speed");
    $SpeedTest--;
}
if ($APIkbpersec > 30) {
    global $SpeedTest;
    echo ("High speed");
    $SpeedTest++;
}
echo $SpeedTest;

the page this code is in gets reloaded every second with AJAX and the $APIkbpersec changes between 40 and 0.

I basically want to have a variable ($SpeedTest) increase or decrese depending on what $APIkbpersec is.

  • if $APIkbpersec is less than 30, I want $SpeedTest to decrease by 1 every refresh to a minimum of 0.
  • if $APIkbpersec is greaterthan 30, I want $SpeedTest to increase from by 1 every refresh to a maximum of 10.

the problem is I dont know what the porblem is....Im currently trying to write $SpeedTest to a txt file so I can read it in every refresh to do the maths on it every refresh without it being reset in PHP

any help would be appreciated

You should use $_SESSION for that purpose.
See HERE for an explanation, but basically you would need to do the following:

session_start();
$SpeedTest = isset($_SESSION['speedTest']) ? $_SESSION['speedTest'] : 0;

if ($APIkbpersec < 30)
{
    echo ("Slow speed");
    $SpeedTest--;
}

if ($APIkbpersec > 30)
{
    echo ("High speed");
    $SpeedTest++;
}
$_SESSION['speedTest'] = $SpeedTest;

echo $SpeedTest;

It's being reset because the HTTP request is stateless. Each AJAX call is an isolated event to a PHP script. To make the variable persist, it has to be stored in $_SESSION.

You have not shown the code you're using to write it to a text file, but unless you need it to persist beyond a user session, that's the wrong approach. You're better served using $_SESSION. If you do need long-term persistence, you should use a database instead.

session_start();

// Initialize the variable if it doesn't exist yet
if (!isset($_SESSION['SpeedTest'])) {
  $_SESSION['SpeedTest'] = 0;
}

echo ("<br/>");
if ($APIkbpersec < 30) {
  echo ("Slow speed");
  $_SESSION['SpeedTest']--;
}
if ($APIkbpersec > 30) {
  echo ("High speed");
  $_SESSION['SpeedTest']++;
}
echo $_SESSION['SpeedTest'];

Either:

  • Return $SpeedTest in the response and pass it back and forth.
  • Use some kind of persistent storage such as a cookie or PHP sessions.

Both are pretty easy to implement. If you want with persistent storage, I'd suggest a cookie as both JS and PHP could share it. Session, although the obvious candidate, are a bit overkill in this case - IMO.

If this is all of your code, the problem is simple. Each time the script is run, the values of all the variables are initialized. For your case, this means that the value of $SpeedTest does not persist - it's reset to zero each time the script is called. You can use a session as @Michael suggests (probably my recommendation), read the value out from a text file or database and then write a new value out, or you could return the value of $SpeedTest to your AJAX script and pass it back into the php script as a parameter. Each of these have various advantages and disadvantages, but using the $_SESSION superglobal is easy to do and takes little modification to your code.

If you want to do it with files you can use a single file to store a single global value for your variable:
Read data from file (docs here):

$data= file_get_contents('file.txt');

Put data into file (docs here)

$bytesWritten = file_put_contents( $data );

Else you can use sessions or database as other suggested.
Without cookies or sessions you cannot have a real "per user" solution so if you need that stick with other answers or use an hybrid solution with sessions/files
If you use the request solution (that kind of ping-pong with POST or GET variables) always pay attention because those variables can be altered by users.
Other things to remember:

  1. Files and database records last until you delete them (so maybe you have to manage undeleted files or records).
  2. Session duration is configured within your server (so they can last too short if you need long term persistency).
  3. Usually database are better than files (the do more tasks and provide your application more scalability) but in some cases files solution is faster (tested) specially if your database resides in another host and is not on the same host as your webserver.