0

Hello guys I have the following problem where a fix in php

?php
  $fruits = array ("Apple", "Blackberry", "Strawberry");
  $tablerows = "";
   foreach ($fruits as $key => $value) {
       $rows = ""; 
       $rows = '<tr>
                    <td>'.$value.'</td>
                </tr>';
       $tablerows = $rows;
  }
  echo  $tablerows;
?>

this is the result

<tr>
        <td>Strawberry</td>
</tr>

and what I expect should be like this

<tr>
        <td>Apple</td>
</tr>
<tr>
        <td>Blackberry</td>
</tr>
<tr>
        <td>Strawberry</td>
</tr>

Anyone who can help me solve this problem would be very grateful.

4
  • 1
    Use .= not =. to append to a string. Commented Jul 20, 2022 at 23:43
  • 1
    There's no need for $rows = ""; since you overwrite it on the next line. Commented Jul 20, 2022 at 23:44
  • Please do not use stacksnippet formatting for server-side code. Commented Jul 20, 2022 at 23:55
  • Friend thank you very much it works in one, you are the best Commented Jul 20, 2022 at 23:58

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.