5

I read about this Article I wondering about this part:

try {
    //First try getting our user numero uno
    $user = new User(1);

    //Then, let's try to get the exception
    $user2 = new User('not numeric');
} catch( Exception $e ) {
    echo "Donkey Kong has caught an exception: {$e->getMessage()}";
}

why {$e->getMessage()} must be covered in bracket?
Is there any link explanation on php manual about this?

i know if we don't put bracket it will show error, but want i want is to get the explanation why bracket is needed.

Notice: Undefined property: Exception::$getMessage

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
GusDeCooL
  • 5,639
  • 17
  • 68
  • 102

6 Answers6

20

It has to do with the fact that this is a complex expression. If you don't include the bracket, i.e.:

echo "Donkey Kong has caught an exception: $e->getMessage()";

...then how does PHP know what variable you're trying to output? You could be trying to do any of the following:

  • output the value of $e, followed by the string "->getMessage()"
  • output the value of $e->getMessage, followed by the string "()"
  • output the value of $e->getMessage()

Placing the brackets around the full expression tells PHP the full expression that needs to be evaluated and included in the string.

Matt Huggins
  • 81,398
  • 36
  • 149
  • 218
3

You can avoid using curly braces by moving your statement out of the quotes:

echo 'Donkey Kong has caught an exception: ' . $e->getMessage();

But to answer your original question: curly braces are needed to group variable names together, so that PHP understands what you're trying to echo out. Without curly braces, PHP treats the statement as if you're printing out the property $e->getMessage, and a literal string '()', which is not what you intended, of course.

See the Stack Overflow question PHP curly brace syntax for member variable, for more information.

I personally recommend simply concatenating strings using the syntax I provided above.

Community
  • 1
  • 1
Kevin Ji
  • 10,479
  • 4
  • 40
  • 63
1

Curly braces are used in PHP to output variables in strings. See Curly braces in string in PHP for a more detailed explanation.

In this case, $e is a variable, and you want to output the result of $e->getMessage(). Without curly braces you would see the actual string Donkey Kong has caught an exception: $e->getMessage().

Community
  • 1
  • 1
Bleaourgh
  • 1,276
  • 9
  • 3
1

That's the way you would insert complex syntax in a php string. You can read more about that here

Deleteman
  • 8,500
  • 6
  • 25
  • 39
1

http://php.net/manual/en/language.types.string.php

Complex (curly) syntax

This isn't called complex because the syntax is complex, but because it allows for the use of complex expressions.

Any scalar variable, array element or object property with a string representation can be included via this syntax. Simply write the expression the same way as it would appear outside the string, and then wrap it in { and }. Since { can not be escaped, this syntax will only be recognised when the $ immediately follows the {. Use {\$ to get a literal {$. Some examples to make it clear:

jn1kk
  • 5,012
  • 2
  • 45
  • 72
0

Because php need to know, it is only text or code. I would better do like this.

echo "Donkey Kong has caught an exception:".$e->getMessage();
Arthur
  • 11
  • Well, if you want to echo array. echo 'Text $arr['item']'; This won't echo, thats why we use echo 'Text {$arr['item']}'; – Arthur Dec 29 '11 at 18:37
  • Im sorry. But actually, i'm not asking about what better code is needed. But why in that tutorial they using bracket cause i'm new about using bracket part in PHP. And maybe with bracket i can improve my coding style. – GusDeCooL Dec 29 '11 at 18:42
  • When you don't echo single variable, and it stores only string, then you can do like that. echo 'Text $text'; If you need to call function or do any other activites, then you should user these brackets. – Arthur Dec 29 '11 at 18:44