Error in declarations with Array

1

Well, I have an error that I can not find the solution. I have two hosting in the same place Hostgator, in a hosting my application is all right and working. But I needed to migrate the application to another address, in other words I migrated to a Subdomain, until then ok.

After I migrated I realized I was giving the following error:

$arr = [];

So I changed the declaration to

$arr = array();

This way it worked in parts, but I realized that you are giving an error in this type of statement, why?

Another problem is being in this next item:

Parse error: syntax error, unexpected '[', expecting ')' in /

In line with this error is the following statement:

if(!empty($ClientesRef->getNome()[0]))

I check if the first position of the array saved in this method is different from empty, I started to see the error only when I migrated to the Sub-domain.

Has anyone ever gone through this and can you help me?

Thank you!

    
asked by anonymous 09.08.2016 / 15:39

3 answers

1

Two problems, but the main problem is: "What is the version of your PHP?"

Empty and arbitrary expressions

The empty only accepts arbitrary expressions from the 5.5 version of PHP.

In PHP 5.5 or higher this is allowed:

if (empty(call_function()) return false;

In previous versions, it is not accepted, and will generate the following error.

  

Can not use method return value in write context

Array or String Dereferencing in function or method calls

This feature allows you to access, by capturing the index, the value of a array or string . This functionality is only available from version 5.4 of PHP.

For example:

 $ClientesRef->getNome()[0];

 function meu_array() {
     return array(1, 2, 3);
 }

 function minha_string() {
    return 'Wallace';
 }


 meu_array()[0]; // int(1);

 minha_string()[0]; // string(W);

In previous versions, this will generate a syntax error.

How do I know the version of PHP I'm using?

If you are using Linux development environment, just use the following command in the terminal:

 php -v

This command also works on Windows, but you have to set the environment variables for PHP to work.

You can also do this by running a script with the following code:

 exit(PHP_VERSION);
    
09.08.2016 / 15:53
0

The code syntax error seems to be related to the php version.

This syntax is only available from php5.4

$ClientesRef->getNome()[0]

To get around this, create a new variable and test it on if.

$nome = $ClientesRef->getNome();
if(!empty($nome[0]))
    
09.08.2016 / 15:52
0

The short syntax for array was introduced in version 5.4.
Syntax error is issued in lower versions.
See: link

It is still early to use shorthand [] since many hosting services still have PHP lower than version 5.4. The safest thing to do is array() .

// Ainda cedo usar isso, caso o seu sistema for instalado em ambientes diversos, especialmente com PHP inferior ao 5.4
$arr = [];

// use assim:
$arr = array();

Array dereferencing

This is also not very suitable because it is a feature introduced in PHP5.4

if(!empty($ClientesRef->getNome()[0]))

To fix:

$arr = $ClientesRef->getNome();
if (!empty($arr[0])) {
    //faz os cambalacho aqui e tal
}

See: link

note: If you are sure that your project will never run under an environment with PHP below 5.4, you do not have to worry and you can use whatever you want.     

09.08.2016 / 15:51