The problem is that the value of the test variable is not displayed the first time. In theory, it should be so. If the condition if(!$test) register if(!$session->get('test')) , then everything is OK, but it is not suitable.

How to make my method display everything from the first time, and not from the second?

 $test = $session->get('test'); if(!$test) { $session->add('test', 'asdasd'); } echo $test; 

    1 answer 1

    Well, when you redefined the variable in the session, you did not redefine it in the script.

     $test = $session->get('test'); if(!$test) { $session->add('test', 'asdasd'); $test = $session->get('test'); } echo $test;