PHP变量随机变为NULL

PHP变量随机变为NULL,php,variables,null,hosting,Php,Variables,Null,Hosting,很长一段时间以来,我们的托管服务器出现了一个非常奇怪的问题。PHP中的变量有时(似乎是随机的)变成空值 一般来说,一切都很好,但偶尔会发生。服务器上的所有帐户和所有PHP应用程序(包括PHPMyAdmin、Wordpress和我们自己的脚本)都会受到影响。我们联系了我们的托管公司,但他们无法找到任何解决方案 我几乎没有什么想法,最有希望的是和苏霍辛的问题。但是我没有直接从日志中得到任何消息 我们制作了一个尽可能简单的脚本来重现错误: <?php class Example { p

很长一段时间以来,我们的托管服务器出现了一个非常奇怪的问题。PHP中的变量有时(似乎是随机的)变成空值

一般来说,一切都很好,但偶尔会发生。服务器上的所有帐户和所有PHP应用程序(包括PHPMyAdmin、Wordpress和我们自己的脚本)都会受到影响。我们联系了我们的托管公司,但他们无法找到任何解决方案

我几乎没有什么想法,最有希望的是和苏霍辛的问题。但是我没有直接从日志中得到任何消息

我们制作了一个尽可能简单的脚本来重现错误:

<?php
class Example
{

    protected $stringVar = 'this is a string value';

    public function accessParameter()
    {
        $error = false;
        if (isset($this->stringVar) && !is_null($this->stringVar)) {
            echo "string var : " . $this->toStringWithType($this->stringVar) . "\n";
        } else {
            echo "string var is not set\n";
            $error = true;
        }

        if ($error) {
            $logfile = dirname(__FILE__)."/random_bug_log.log";
            file_put_contents($logfile, date('Y-m-d H:i:s')."\n", FILE_APPEND);
            file_put_contents($logfile, $this->toStringWithType($this->stringVar) . "\n", FILE_APPEND);
        }
    }

    public function toStringWithType($var)
    {
        $type = gettype($var);
        return "($type) '$var'";
    }

}

$e = new Example();
$e->accessParameter();
奇怪事情发生时的输出:

string var is not set
string var is not set
string var is not set
我愿意接受任何关于如何解决这个问题的想法或建议。我想最终的解决办法是改变托管公司。我没有设法在本地主机或任何其他服务器上创建此问题


已制作的试件,包括您的建议:

<?php
class Example
{
  protected $stringVar = 'this is a string value';

  public function accessParameter() {
    $error = false;
    if(isset($this->stringVar) && !is_null($this->stringVar)) {
      echo "string var : "
    .$this->toStringWithType($this->stringVar)
    ."\n";
    } else {
      echo "string var is not set\n";
      $error = true;
    }

    if($error) {
      $logfile = dirname(__FILE__)."/random_bug_log.log";
      file_put_contents($logfile, date('Y-m-d H:i:s')."   ", FILE_APPEND);
      file_put_contents($logfile, 
            $this->toStringWithType($this->stringVar) . "\n",
            FILE_APPEND);
    }

  }

  public function writeParameter() {
    $this->stringVar="variable assigned";
    if(isset($this->stringVar) && !is_null($this->stringVar)) {
      echo "string var : "
    .$this->toStringWithType($this->stringVar)
    ."\n";
    } else {
      echo "string var is not set\n";
      $error = true;
    }
  }

  public function toStringWithType($var)
  {
    $type = gettype($var);
    return "($type) '$var'";
  }


}

$e = new Example();
$e->accessParameter();
$e->writeParameter();

这是一个非常奇怪的问题

这可能不是一个解决办法,但值得一试

protected $stringVar;

function __construct() {
    $this->stringVar = 'this is a string value';
}

我建议您使用!==而不是为null,以查看变量是否实际为null

if(isset($this->stringVar)&($this->stringVar!==null)){

if(isset($this->stringVar)&&(!empty($this->stringVar)){


也应该做这项工作。

如果出现此类问题,请检查if条件中的值,然后在其他条件中执行您想要的操作。如在您的情况下,请执行以下操作:

 if(isset($this->stringVar) && ($this->stringVar == "this is a string value")) {

 }else{
  // your code here...
 }

这是一个非常奇怪的情况。我建议看看他们是否可以改变你的服务器,而不是改变公司,这可能是一些间歇性硬件故障或即将发生故障的迹象,例如内存芯片
isset-确定变量是否设置且不为NULL
。因此,你不需要检查
是否为NULL
。我也遇到过这样的问题奇怪的问题,例如随机
strpos
返回
false
。@Amir是的,你不能在没有检查通知的情况下将
NULL
与未定义区分开来。@zupapomidorowa-如果你在某个地方写入变量(此时它是静态的),这个问题也会出现吗?
 if(isset($this->stringVar) && ($this->stringVar == "this is a string value")) {

 }else{
  // your code here...
 }