1

I have this PHP code and getting this error:

Parse error: syntax error, unexpected '$e' (T_VARIABLE)

In this line:

$error = echo 'Captured: ',  $e->getMessage(), "\n";

I got this information from here. I just wanted to save the echo to a variable. What am I doing wrong here?

1
  • Comment to the close voter: while the error message is caused by the typo in the first instance, when the commas are replaced with a period there is the further problem of echo which doesn't need / want to be there. Commented Jan 18, 2014 at 15:15

1 Answer 1

8

Comma is not a concatenation operator in PHP, Period is. Secondly, echo doesn't return the string back, it only outputs it. Remove the echo and save your string in your variable like this:

$error = 'Captured: '.  $e->getMessage(). "\n";

Now you may wonder that if this is the case then why do you have an example on PHP.net having comma there?

echo 'Captured: ',  $e->getMessage(), "\n";

It is because that is not string concatenation, those are 3 different parameters being sent to the echo command so in that case it is valid syntax, but for string concatenation it wont be.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.