Suppose there is a $ _SESSION ['name'] superglobal array. It contains the following data: GrishaMashaPasha Ie all in porridge. I need to read these names and write them in a new line. Those. to have 3 lines.

So I do this: I add the symbol ';' to each name it turns out with us

Гриша;Маша;Паша; 

Next, I divide the names I need function

 explode(';',$_SESSION['name']) 

and run the cycle

 foreach ($_SESSION['name'] as $value) { $peremennaya = '<tr>'.$value.'</tr>'; } 

And here the PROBLEM he displays to me only the last meaning i. Pasha And I need to display in the following form: Grisha Masha Pasha

Where is the mistake? I just need to write the values ​​in the new lines from the session!

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

Do you run a loop on $ _SESSION ['name'], in it is the result of the function explode (';', $ _ SESSION ['name']) or the original string? In addition, since you have three values, you need three variables or three array elements for them.

 $arr = explode(';',$_SESSION['name']); $peremennaya = []; foreach ($arr as $value) { echo $value."<br />"; $peremennaya[] = $value; } echo $peremennaya[0]."<br />"; echo $peremennaya[1]."<br />"; echo $peremennaya[2]."<br />"; 
  • Thank you very much, I think this is what you need! And my mistake was that I did not write each value to a new array, namely, I forgot something like this $ peremennaya [] = $ value; - Mr. Nomov 7:04 pm
  • Unfortunately, the identity of something is wrong, I need to take each value from an array or from a super global array and place my code in a table: if (! Empty ($ _ SESSION [names]] {explode .... Foreach (session explode as $ value) {$ tr [] = <tr> Value </ tr>} and at the bottom I output $ tr but hint 25 in the html, the last value is displayed in the table, and all values ​​should be added from the new line - Mr. Nomov

It is better to create an empty variable at the beginning and then supplement it.

 $peremennaya = "" foreach ($_SESSION['name'] as $value) { $peremennaya .= '<tr>'.$value.'</tr>'; } 

In your code, the variable is constantly changing and it is logical that it displays the last substituted value. Operator. = Concatenates the variable and the string that comes after the operator.