And what, uninitialized session variables cause exceptions when the script is running? Something I have never heard about it - but I uploaded the site to the hosting and went to the problems. Do I have to declare variables via session_register() ?

  • describe what kind of problems, perhaps this is not the case - Gedweb
  • My fields are filled in with session variables: <input type = "text" value = <? = $ _ SESSION ['text']? >> This variable is not initially initialized, in theory, nothing should be output, but actually output of type <br /> <br />: Undefined index: text in mySite.php on line - Deus
  • @Deus, in theory, you just should get an error about an undefined array key. - xEdelweiss
  • What to do - probably so? session_start(); $_SESSION['text'] = ""; Those. All session variables are initially defined? - Deus
  • one
    or request them somehow like this: <?=isset($_SESSION['text'])?$_SESSION['text']:''?> - xEdelweiss

2 answers 2

Undefined index: text in mySite.php on line is not an error. The PHP interpreter simply displays a warning that the variable is not defined (more precisely, that there is no such index as "text" in the $ _SESSION array).

As the interpreter is simply obliged to warn about the presence of such an error, and you, for your part, are simply OBLIGED to do something like

 if(isset($_SESSION['text'])) { //вывод значения } else { // вывод пустой строки } 
  • Thank you, to be honest, nobody warned me about it, I thought that PCP is so flexible that if a variable is not defined, it simply does not output anything. Without any errors and warnings - Deus
  • However, I was engaged in books. And there are half neskazyvayut - Deus
  • @Deus, throw away these books and take some others. - xEdelweiss
  • I've done this a long time ago. - Deus

now everything is clear, you are trying to refer to an element of the array that is not declared. There can be no "idea" here. Perform checks before output via isset($_SESSION['text'])

try not to store in sessions anything and control the presence of variables you are working with


  <input type="text" value="<?=(isset($_SESSION['text'])?$_SESSION['text']:'')?>"> 
  • one
    @KryDos, ahead. Well, let it be now) - Gedweb
  • one
    @Gedweb, and your answer is even nicer :) The ternary operator looks much shorter than my version. - KryDos