0

我正在从Project Euler中休息一下,学习一些 PHP/HTML 来踢球和咯咯笑,我发现了一页简单的练习。所以,在我的“网站”上,我希望有一个有序的链接列表,指向每个练习的页面,但我决定以动态的方式进行,而不是在我做练习时对每个项目进行硬编码。不幸的是,应该包含列表的页面根本不呈现!

假设我的系统上有名为“exawk#.php”的文件,这段代码还有什么问题?对不起,如果它是草率或可怕的,这实际上是我网络编程的第一天。

<html>
  <head>

    <title> Awaken's Exercises </title>

  </head>

  <body>

    <h1>This page contains "Awaken's Exercises" from
    <a href="http://forums.digitalpoint.com/showthread.php?t=642480">
    this page</a>.</h1>

    <ol>
    <?php
      $arex = glob("exawk*.php"); // $arex contains
                                //an array of matching files
      $numex = 0;
      $i = 0;
      foreach( $arex )
      {
        $numex++;
      }

      while( $numex >= 0 )
      {
        echo "<li><a href=" .$arex[$i].
             ">Problem #" .$numex. ".</a></li>";
        $numex--;
        $i++;
      }

    ?>
    </ol>

  </body>

</html>
4

1 回答 1

1

display_errors在 php.ini中启用:foreach( $arex )是语法错误(缺少.. as $varname)。

从命令行,您可以使用php -l /path/to/your/file.php.

此外,此示例:

  //an array of matching files
  $numex = 0;
  foreach( $arex as $youdontdoanythingwiththis)
  {
    $numex++;
  }

可能:

 $numex = count($arex);

更好的整个事情:

while( $numex >= 0 )
{ ...etc

可能:

$num = 1;
foreach($arex as $file){
    echo '<li><a href="'.$file.'">Problem #'.$num.'</a></li>';
    $num++;
}
于 2010-08-04T15:54:57.247 回答