0

I didnot understand why this is not working in PHP 7.0

$obj = mysqli_fetch_object(...);
if (is_object($obj)) { // OK
 echo $obj->name;
 // writes Hello
 $my_var = "name";
 echo $obj->$my_var; 
 // writes Hello
 $my_arr = array('test' => array('col' => 'name'));
 echo $obj->$my_arr['test']['col']; // <-- this didnot work
 // writes nothing :( 
}

Could I anyway to correct it?

2
  • Because -> is executed before [...]. Commented May 22, 2017 at 16:33
  • How could I correct it? Commented May 22, 2017 at 16:34

1 Answer 1

1

-> is executed before [], so your code is equivalent to:

echo ($obj->$my_arr)['test']['col'];

You can change that with {}:

echo $obj->{$my_arr['test']['col']};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.