ReactJS 教程中的示例"server.php"存在语法错误


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

我开始学习React,在正式教程的一开始,它需要一个正在运行的服务器。

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

此外,我将教程中所有需要的示例文件(包括"server.php")放在本地的"/var/www/react tutorial master"文件夹中。

最后,在终端中,我输入上面的文件夹,并按照教程中的说明执行"php-server.php"。

但是,终端告诉我

"PHP分析错误:语法错误,中出现意外的'[]/var/www/areact tutorial master/server.php第37行">

我没有对文件进行任何mdodification。我不熟悉PHP,但我不认为示例"server.PHP"有任何语法错误。

我很可能错过了什么,你能告诉我我错过了什么吗。

这是一张描述我的问题的屏幕截图。

非常感谢!

"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行为

$comments解码[]=[

PHP 5.4.0提供了一系列新功能:

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服务器。

最新更新