0

I'm trying this: print "This is my car number $this->attr['number'] ... \n";

But i get this error: Array to string conversion ...

How can i solve this syntax error and use " instead of '?

8
  • This is because $this->attr['number']is an array. Just var_dump() it and use the key for which you want the value to be printed. Commented Jul 25, 2015 at 21:08
  • 1
    @D4V1D you are wrong. PHP is evaluating $this->attr as the variable. Commented Jul 25, 2015 at 21:11
  • @rahultyagi read my last comment. Commented Jul 25, 2015 at 21:13
  • Err, no! Again, $this->attr['number'] outputs an array, just as PHP just told you. You should access values of this array by doing $this->attr['number']['key']. Commented Jul 25, 2015 at 21:13
  • 1
    @D4V1D sorry but again, you are wrong. Please read the PHP manual (php.net/manual/en/language.types.string.php) and Ctrl+F for "Complex (curly) syntax". Commented Jul 25, 2015 at 21:16

1 Answer 1

4
print "This is my car number {$this->attr['number']} ... \n";

Putting variable in {} should solve your problem, because php evaluate it as one variable

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! More info at php.net/manual/en/language.types.string.php (Complex (curly) syntax).

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.