1

thank u for taking time to read my question. It is probably a beginner level question, but I did a lot of search without finding an answer.

I am starting to learn React, and at the very beginning of the official tutorial, it requires a running server.

I have Apache installed and running in my Ubuntu System.

Also, I put all needed example files from the tutorial including the "server.php" in my local "/var/www/react-tutorial-master" folder.

And finally, in the terminal, I entered to the folder above, and executed "php server.php" as the tutorial says.

BUT, the terminal tells me that

"PHP Parse error: syntax error, unexpected '[' in /var/www/react-tutorial-master/server.php on line 37 "

I didn't do any mdodification to the file. And I am not familiar with PHP, but I don't think the example "server.php" has any syntax error.

So propably I am missing something, could you please tell me what I am missing.

Here is a screenshot that describes my problem.

Thanks a lot!

The "server.php" looks like this:

<?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;
    }
}

Line 37 is

$commentsDecoded[] = [

Yutian Li
  • 13
  • 2

1 Answers1

1

PHP 5.4.0 offers a wide range of new 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.

Solution:

Uninstall your WAMP Server, download and install Latest version of Wamp Server.

Muhammad Shahzad
  • 9,340
  • 21
  • 86
  • 130