PHP variables randomly become NULL

For quite some time, we have encountered a very strange problem with our hosting server. From time to time (it seems random) the variables in PHP become NULL.

In general, everything works fine, but this happens from time to time. All accounts on the server are affected and all PHP applications (including PHPMyAdmin, Wordpress are our own scripts). We contacted our hosting company, but could not find a solution.

I had few ideas, the most promising was the question with Sukhosin. But I do not receive the messages in the journal directly from it.

We made the simplest possible script to reproduce the error:

<?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('Ymd 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(); 

Normal output:

 string var : (string) 'this is a string value' 

Conclusion when a strange thing happens:

 string var is not set 

I open any ideas or suggestions on how to solve this problem. I think the ultimate solution is to change the hosting company. I was not able to create this problem on the local host or on any other server.


Test sample produced, including your suggestions:

 <?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('Ymd 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(); 

Conclusion when the following happens:

 string var is not set string var is not set 
+4
source share
3 answers

This is a very strange problem.

this may not be a solution, but worth a try;

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

I would recommend using! == instead of is_null to see if the variable is really null.

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

or

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

should also do the job.

0
source

In the case of abstracts such as questions, check the meaning that you have if the condition and do what you want otherwise. As in your situation, follow these steps:

  if(isset($this->stringVar) && ($this->stringVar == "this is a string value")) { }else{ // your code here... } 
0
source

Source: https://habr.com/ru/post/1480932/


All Articles