简体   繁体   中英

Syntax working on localhost, not working on godaddy server

Can someone tell me why this code would work on my local server but not with godaddy? I get the following error on godaddy...

Parse error: syntax error, unexpected '[' in /home/content/20/10592020/html/foodport/application/controllers/register.php on line 41

Here is the get_userId function from my user_model .

public function get_userId(){
        $results = $this->obtain_user_info('users', array('username'=>$this->username), true);

        if(count($results) > 0)
        {
            return $results;
        }else
        {
            return array("user_id" => GUEST_ID);
        }
    }

Here is the obtain_user_info function:

private function obtain_user_info($table, $params, $assoc = false){
        $query = $this->db->get_where($table, $params);

        if(!$assoc){
            $results = $query->result();
        }else
        {
            $results = $query->row_array();
        }

        return $results;
    }

Here is the code in my register controller specifically line 41.

$user_id = $user->get_userId()['user_id'];

Check the version of PHP - my guess is that your local server is running a later version than GoDaddy's server. You might have to do something like this:

$temp_id = $user->get_userId();
$user_id = $temp_id['user_id'];

Specifically, you need PHP > 5.4.0 to access a function return array directly like:

$user_id = $user->get_userId()['user_id'];

Prior to 5.4.0 you would need to assign the return of the function to a variable and access it:

$result  = $user->get_userId();
$user_id = $result['user_id'];

But I would ask, why return an array if you are only returning one value?

$user_id = $user->get_userId()['user_id'];

http://php.net/manual/en/language.types.array.php#example-88

This is Array dereferencing which is available in php 5.4 However usually godaddy got php 5.3.3

Depending on your hosting plan you can change it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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