Error in declarations with Array

Asked

Viewed 93 times

3

Guys, I have a mistake that I can’t find the solution to. I have two accommodations on the same site Hostgator, in a hosting my application is all right and working. But I had to migrate the application to another address, in other words migrate to a sub-domain, until then ok.

After I migrated I realized I was making a mistake in the following statement:

$arr = [];

So I changed the statement to

$arr = array();

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

Another problem is being in this following item:

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

On the line of 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, the error started to appear only when I migrated to the Sub-domain.

Someone’s been there and can help me?

Thanks!!!

  • In error, which appears after the in /? Does the error say which line is the error on? What’s the previous and next line?

  • Which php version you use?

3 answers

3


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

Empty and arbitrary expressions

The empty only accepts arbritarias expressions from version 5.5 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’t use method Return value in write context

Array or String Dereferencing in function calls or methods

This functionality allows you to access, through the index capture, the value of a array or string. This feature 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 to know the PHP version I’m using?

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

 php -v

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

It is also possible to perform this check by running a script with the following code:

 exit(PHP_VERSION);
  • could explain better this array Indice business in function?

  • @Rafaelacioly added!

  • Thank you for your reply, I will check the version, nor have I tended to this.

1

Short array syntax was introduced in version 5.4.
In lower versions is issued syntax error.
Behold: http://php.net/manual/en/migration54.new-features.php

It’s still early to use the shorthand [] because many hosting services still have PHP lower than version 5.4. The safest is to use 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 as it is a feature introduced in PHP5.4

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

To correct:

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

Consult: http://php.net/manual/en/language.types.array.php

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

  • Thank you for your reply, I will check the version, nor have I tended to this.

1

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

This syntax is only available from php5.4

$ClientesRef->getNome()[0]

To work around this, create a new variable and test it in if.

$nome = $ClientesRef->getNome();
if(!empty($nome[0]))
  • Thank you for your reply, I will check the version, nor have I tended to this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.