weird php array syntax errors - php

I have:
on my pc:wamp server (apache+php-5.4.3+mysql)
on my vps:nginx+php5-fpm+mysql(all latest in the ubuntu 64 bit server repository).
recently I have reinstalled the whole vps
my problem is,
statement like this:
function foo($arr){
return $arr;
}
foo(['foo1','foo2','foo3'])
works on my pc, and used to work on the server (same installation) but now it gives
an error such as:
Parse error: syntax error, unexpected '[' in /home/Websites/Domains/squazza/php/db.php
on line 261
(replacing the [ ] with Array( ) does fix the problem)
now I assume that this problem is related to php.ini so i am asking what setting could possibly be affecting this kind of behavior and why?

If you look at the documentation that short syntax is only avaible in php 5.4+ I suspect your php versions are different.
This list of new features in PHP5.4 maybe of some use to you.

You're probably not using PHP 5.4. Run phpinfo() in your script and check it. And remember, you can have different PHP version in CLI mode.

As pointed out by #piddl0r, the short array syntax ([ 1, 2, 3 ]) was added in PHP 5.4. You'll note from The Ubuntu Package Archive that Ubuntu 12.04 - Precise Pangolin - only provided PHP 5.3.10; PHP 5.4.6 was provided in the next release, Quantal. To check your version of PHP, use phpinfo() or php --version on the command line.

Related

Why I get error in Redhat linux for CodeIgniter REST_Controller

I have written a code in CodeIgniter and it is running on Ubuntu but when I try to run it in Redhat it gives me the error below :
syntax error, unexpected '[' in /var/www/html/dgfp-v1/application/libraries/REST_Controller.php on line 32
Please help me.
I agree with mdamia, it should be something to do with PHP version. I have the same issue and after replacing the [] with array().
I've received another error Using PHP v5.3.24, though PHP v5.4 or greater is required
Using PHP v5.3.24, though PHP v5.4 or greater is required
Here it is the REST_Controller.php i have updated.
enter link description here
Hope this help

Debian PHP 5.4.27 vs Ubuntu PHP 5.4.16

Well, I'm transferring a little web server from one computer to other, on the 'other' computer I cannot a more updated version of PHP,
I just have to add my files through ssh and that's all, when I load my webpage it says explicitly:
The error is showing on a Debian with PHP 5.4.27 squeeze, the webserver comes from an Ubuntu with PHP 5.4.16 oneiric
Parse error: syntax error, unexpected '[' in ~directory~ on line 38
This is the line 38.
$VALUE=$DB->query("a nice sentence")->fetch()[0];
So, i was pretty surprised because PDO was accepted since PHP 5.1, so, i look deeper and i found the possible error, you can't simply do ->fetch()[0], you must separate it in order to be "processed",
However, I have transport this server with this information before, so i really don't know if it's something on the configuration of PHP or maybe the version by itself 5.4.X? You guys know any hint? As I said, i can't just install other updated version of PHP5, what do you recommend?
Have a nice night.
Edit: updated to the issue on the PHP 5.4 now
The error seems triggered by the array dereferencing. This was added in PHP 5.4.. Not sure how you made that work in 5.3.6
http://nl3.php.net/language.types.array.php
To workaround you could try
$VALUE = $DB->query("a nice sentence")->fetch();
echo $VALUE[0];
PHP Version on my ubuntu (where it works):
PHP Version 5.4.16
PHP Version on the Debian (where it does not work):
PHP Version 5.4.27-1~dotdeb.0
This version number was extracted by using phpinfo(); on the index page.
Apparently, the error was solved by JUST changing the first issue line (the other hundreds of codes that had that wasn't necessary to change, what i believe to be a little weird).
$VALUE = $DB->query("a nice sentence")->fetch()->offset(0);

syntax error redirects to a 'Server not found' page

I am trying to access an element of an array in PHP using the following method:
$test = $this->Student->find('first',array('conditions',array('Student.student_table_id'=>1)))['Student']['student_id'];
It seems to work well on a slightly newer version of XAMPP, but when I try to use that syntax on another machine with an older version of XAMPP for Mac OS X 1.7.3 installation, it throws me to a Server not found page. I don't know if it something to do with my XAMPP configuration or something else..
Also, this behavior, i.e. redirection to a server not found page, happens whenever there is a syntax error.
Mind you, both machines support the minimum requirements of CakePHP.
From the comment..
the PHP Version on the problematic machine is 5.3.1
That is because you are trying to use a new feature of PHP 5.4 , called the function array dereferencing.
How to fix ?
Break down your code like this.
$test = $this->Student->find('first',array('conditions',array('Student.student_table_id'=>1)));
$test1 = $test['Student']['student_id'];

MSSQL.so for PHP 5.3 not loading (CentOS 5.8)

I'm trying to install the mssql module for PHP on a server running CentOS 5.8 and PHP 5.3.5. I was able to get it working on the dev server which runs CentOS 5.2 and PHP 5.2.6, and everything seems to match up (FreeTDS installed, module in the modules folder, extension=mssql.so added to php.ini). However, the plugin won't load; doesn't show up in phpinfo() and I get an undefined function error on the test script.
Also of note: phpinfo() lists '--with-mssql=shared,/usr' under the configure command, which is really confusing me.
Solved. Not 100% what the fix was, but I'm pretty sure this was what fixed it.
First off, it probably didn't help I was trying to compile from a copy of php 5.3.16 instead of 5.3.6. Even then, I was running into errors trying to compile it. I found an fix that said to remove this line from php_mssql.h in the folder after it's phpized:
typedef unsigned char *LPBYTE;
I was then abe to do make && make install and it loads with no errors now.

What version of PHP started supporting the following syntax?

on my server (Centos5) i have PHP 5.2, i'm unable to update it, i read somewhere that PHP 5.4 won't work on Centos5, didn't inspect enough as i am in the middle of a lot of projects, anyways, here is the syntax :
$data['trees'] = (new Tree())->where('parent_id',0)->get();
On my WAMP it runs on PHP 5.4.3, on the server it gives me the following error:
Parse error: syntax error, unexpected T_OBJECT_OPERATOR
Support for accessing instance members directly after a new expression was introduced in PHP 5.4 (finally). Note that the parentheses are required around the new expression, as in your given line of code, or it won't work.
I haven't heard of any particular server configurations or platforms where this won't work when running PHP 5.4. If it causes an error on a server where PHP 5.4 is installed, it's likely that's not the version that's interpreting your script (check your server config, .htaccess, and INI directives).

Categories