echo输出“0”而不是表

时间:2011-10-31 21:18:38

标签: php database

请看下面的代码:

<?php
    $output += "
    <table border='1'>
          <tr>
          <th>Session ID</th>
          <th>TeacherUsername</th>
          <th>Teacher Name</th>
          <th>Module Number</th>
          <th>Module Name</th>
          <th>Course ID</th>
          <th>Course Name</th>
          <th>Year</th>
          <th>Student Username</th>
          <th>Student Name</th>
          <th>Mark</th>
          <th>Grade</th>
          </tr>
    ";
           $total = 0;
            $count = 0;
            while ($row = mysql_fetch_array($result)) {
            $count++;
              $total += $row['Mark'];
              $output += "
          <tr>
          <td>{$row['SessionId']}</td>
          <td>{$row['TeacherUsername']}</td>
          <td>{$row['TeacherForename']} {$row['TeacherSurname']}</td>
          <td>{$row['ModuleId']}</td>
          <td>{$row['ModuleName']}</td>
          <td>{$row['CourseId']}</td>
          <td>{$row['CourseName']}</td>
          <td>{$row['Year']}</td>
          <td>{$row['StudentUsername']}</td>
          <td>{$row['StudentForename']} {$row['StudentSurname']}</td>
          <td>{$row['Mark']}</td>
          <td>{$row['Grade']}</td>
          </tr>";
            }


              $output += "        </table>";

        $average = (int)($total/$count);
        echo "<p>Average Mark: $average</p>";
    echo $output;
      ?>

由于某些奇怪的原因,它不能正确回显$output(位于代码底部)。假设输出表格,而不是它回显0,为什么输出0

4 个答案:

答案 0 :(得分:15)

您正在连接JavaScript样式。你想要:

$output = '';
$output .= 'foobar';

答案 1 :(得分:8)

$output += " ..."正在进行ADDITION,而不是连接。您正在向一个数字添加一个字符串,因此PHP会尽可能地将字符串转换为数字,这可能是0

尝试

$output .= "...";

代替。或者更好的是,使用HEREDOC

$output .= <<<EOL
...
EOL;

答案 2 :(得分:4)

使用+=将尝试添加字符串的'value',而不是在PHP中连接它。

改为使用.=

答案 3 :(得分:-5)

不要使用javascript,加上但使用php的点

$output .=

但在使用$output之前,您还必须先定义.=

所以结果:

<?php
    $output = "";
    $output .= "
    <table border='1'>
          <tr>
          <th>Session ID</th>
          <th>TeacherUsername</th>
          <th>Teacher Name</th>
          <th>Module Number</th>
          <th>Module Name</th>
          <th>Course ID</th>
          <th>Course Name</th>
          <th>Year</th>
          <th>Student Username</th>
          <th>Student Name</th>
          <th>Mark</th>
          <th>Grade</th>
          </tr>
    ";
           $total = 0;
            $count = 0;
            while ($row = mysql_fetch_array($result)) {
            $count++;
              $total .= $row['Mark'];
              $output .= "
          <tr>
          <td>{$row['SessionId']}</td>
          <td>{$row['TeacherUsername']}</td>
          <td>{$row['TeacherForename']} {$row['TeacherSurname']}</td>
          <td>{$row['ModuleId']}</td>
          <td>{$row['ModuleName']}</td>
          <td>{$row['CourseId']}</td>
          <td>{$row['CourseName']}</td>
          <td>{$row['Year']}</td>
          <td>{$row['StudentUsername']}</td>
          <td>{$row['StudentForename']} {$row['StudentSurname']}</td>
          <td>{$row['Mark']}</td>
          <td>{$row['Grade']}</td>
          </tr>";
            }


              $output .= "        </table>";

        $average = (int)($total/$count);
        echo "<p>Average Mark: $average</p>";
    echo $output;
      ?>