简体   繁体   English

Amazon EC2 Server上的PHP语法错误(正在开发中)

[英]PHP Syntax Error on Amazon EC2 Server (Works In Development)

I have the following line of code 我有以下代码行

foreach ($_SESSION['ini']->infrastructure()['buildings'] as $key => $value)

The variable $_SESSION['ini'] holds an object which reads various .ini files and holds their values in memory. 变量$ _SESSION ['ini']包含一个对象,该对象读取各种.ini文件并将其值保存在内存中。 It then returns individual sets of .ini variables as arrays when its functions are called. 然后,在调用其函数时,它会将.ini变量的各个集合作为数组返回。 infrastructure() is such a function, and it returns a matrix. Infrastructure()就是这样一个函数,它返回一个矩阵。 I only want the ['buildings'] array out of that matrix. 我只想从那个矩阵中取出['buildings']数组。 The loop works fine on my development server. 该循环在我的开发服务器上工作正常。 However, when I upload the code to the Amazon EC2 server, the server throws the following error: 但是,当我将代码上传到Amazon EC2服务器时,服务器会引发以下错误:

Parse error: syntax error, unexpected '['

I assume that this is a reference to the practice of trying to grab the ['buildings'] array straight from the return value of the function, and indeed, the following code works fine on the EC2 server: 我假设这是对尝试直接从函数的返回值获取['buildings']数组的实践的参考,实际上,以下代码在EC2服务器上可以正常工作:

$placeholder = $_SESSION['ini']->infrastructure();
foreach ($placeholder['buildings'] as $key => $value)

Can anyone explain what would cause this variation in proper syntax between development and production, and how I can fix the EC2 server to work with the development code? 谁能解释在开发和生产之间的正确语法变化的原因,以及如何修复EC2服务器以使用开发代码? I would prefer not to recode everything with the '$placeholder' approach, because it is less efficient, and because I use this syntax a lot throughout the project. 我宁愿不使用'$ placeholder'方法重新编码所有内容,因为它效率较低,而且在整个项目中我经常使用这种语法。

Seems like your PHP version on your EC2 is less than 5.4. 似乎您的EC2上的PHP版本小于5.4。 Function array dereferencing is allowed only after versions from 5.4. 仅从5.4版本开始,才允许Function array dereferencing

The function array dereferencing.. 函数数组解引用

infrastructure()['buildings']

Since you know how to fix it this error, I am not going to write up. 因为您知道如何解决此错误,所以我不会写。 It is just the version why the code didn't work on your EC2. 这只是代码无法在您的EC2上运行的版本。

The issue is most likely with this: 此问题最可能与以下原因有关:

$_SESSION['ini']->infrastructure()['buildings']

Support for function array dereferencing wasn't added until PHP 5.4, so your EC2 instance is most likely running a version lower than that. 直到PHP 5.4才添加对函数数组取消引用的支持,因此您的EC2实例运行的版本可能低于该版本。

See: http://php.net/manual/en/migration54.new-features.php 请参阅: http//php.net/manual/en/migration54.new-features.php

You can fix it by just storing the results of the function in a variable: 您可以通过将函数的结果存储在变量中来解决此问题:

$var = $_SESSION['ini']->infrastracture();
foreach ($var['buildings'] as $key => $value)

Or you can upgrade PHP versions on your EC2 instance if you want to keep using the syntax. 或者,如果您想继续使用语法,则可以在EC2实例上升级PHP版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM