示例" server.php"来自ReactJS教程的语法错误

时间:2016-06-09 03:48:29

标签: php apache reactjs

谢谢你花时间阅读我的问题。这可能是一个初学者级别的问题,但我没有找到答案就做了很多搜索。

我开始学习React,并在official tutorial的最开始, 它需要一个正在运行的服务器。

我在我的Ubuntu系统中安装并运行了Apache。

另外,我把教程中包含的所有必需的示例文件包括" server.php"在我的本地" / var / www / react-tutorial-master"文件夹中。

最后,在终端中,我进入上面的文件夹,执行了#34; php server.php"正如教程所说。

但终端告诉我

  

" PHP解析错误:语法错误,意外' ['在   第37行" / / p>上的/var/www/react-tutorial-master/server.php

我没有对文件进行任何修改。 我不熟悉PHP,但我不认为这个例子" server.php"有任何语法错误。

所以我很遗憾地错过了什么,请你告诉我我错过了什么。

以下是描述我的问题的screenshot

非常感谢!

" server.php"看起来像这样:

<?php
/**
 * This file provided by Facebook is for non-commercial testing and evaluation
 * purposes only. Facebook reserves all rights not expressly granted.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
$scriptInvokedFromCli =
    isset($_SERVER['argv'][0]) && $_SERVER['argv'][0] === 'server.php';

if($scriptInvokedFromCli) {
    $port = getenv('PORT');
    if (empty($port)) {
        $port = "3000";
    }

    echo 'starting server on port '. $port . PHP_EOL;
    exec('php -S localhost:'. $port . ' -t public server.php');
} else {
    return routeRequest();
}

function routeRequest()
{
    $comments = file_get_contents('comments.json');
    $uri = $_SERVER['REQUEST_URI'];
    if ($uri == '/') {
        echo file_get_contents('./public/index.html');
    } elseif (preg_match('/\/api\/comments(\?.*)?/', $uri)) {
        if($_SERVER['REQUEST_METHOD'] === 'POST') {
            $commentsDecoded = json_decode($comments, true);
            $commentsDecoded[] = [
                'id'      => round(microtime(true) * 1000),
                'author'  => $_POST['author'],
                'text'    => $_POST['text']
            ];

            $comments = json_encode($commentsDecoded, JSON_PRETTY_PRINT);
            file_put_contents('comments.json', $comments);
        }
        header('Content-Type: application/json');
        header('Cache-Control: no-cache');
        header('Access-Control-Allow-Origin: *');
        echo $comments;
    } else {
        return false;
    }
}

第37行

  

$ commentsDecoded [] = [

1 个答案:

答案 0 :(得分:1)

PHP 5.4.0提供了各种新的features

Support for traits has been added.
Short array syntax has been added, e.g. $a = [1, 2, 3, 4]; or $a = ['one' => 1, 'two' => 2, 'three' => 3, 'four' => 4];.
Function array dereferencing has been added, e.g. foo()[0].
Closures now support $this.
<?= is now always available, regardless of the short_open_tag php.ini option.

解决方案:

卸载您的WAMP服务器,下载并安装Wamp Server Latest version