0

What is the difference of these two (in the title)?

The first is the use of {$var} vs the use of 'string here'.$var.'another string'

Example of {$var}:

<?php echo "Hello {$var}!;" ?>

Example of 'string'.$var.'string again':

<?php echo '<strong style="color:#009900;">' . $username . ' is OK</strong>'; ?>
  • 2
    dupe: http://stackoverflow.com/questions/5571624/what-does-mean-in-php-syntax –  Oct 01 '16 at 05:31

3 Answers3

0

Below code will help you understand little bit better about the curly braces.

( code tested with PHP 5.5.12 )

  <?php
        $p = 5;
        $r = 6;
        echo "{$p}{$r}"; //56
        $a = "string";
        echo ".$a."; //.string.
        printf(".$b.");//undefined variable
        echo "{}"; //{}
        echo "$c"; //undefined variable
        echo "{{$a}}"; //{string}
        echo "{{$b}}"; //undefined variable
        class a {
            static $x = "xyz";
            const x = "cdef";
            function b() {
                return "World";
            }
        static function d(){
    return "php";
    }
    }
    $xyz = "pqrs";
    $cdef = "defg";
    $c = new a;
    echo "Hello {$c->b()}.\n"; //Hello world.
    echo "Hello {a::d()}"; //{a::d()}
    echo "Hello {${a::d()}}"; //undefined variable
    echo "Hello {${a::$x}}"; //Hello pqrs
    echo "Hello {${a::x}}"; //Hello defg
    function g(){
    return "gun";
    }
    echo "{g()}"; //{g()}
    ?>

What more matters here is the code is more readable and looks better than writing with using concatenation operator ( . ) . And also allows complex expression that is why it is also called Complex (curly) syntax. From here you can understand , this is basically a writing style you should prefer.

Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
0

This is called, "Complex (curly) syntax". And is used to specifically indicate a variable expression in a string with double quotes.

The other one you mentioned exits out of variable parsing in a double quoted string.

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

Robert
  • 10,126
  • 19
  • 78
  • 130
0

As another add-on, {} notation is REQUIRED if you want to use a multi-dimensional array or object inside a "-quoted string. e.g.

$foo[1][2] = 'bar';

echo "hi $foo[1][2] mom";    // prints: hi Array[2] mom
echo "hi {$foo[1][2]} mom";  // prints: hi bar mom

PHP's parser is not "greedy", and once it finds the first [] key for an array variable, it doesn't scan further for more keys. That means the child arrays keys will be ignored, and now you're printing an array in string context, which is simply the literal word Array.

Ditto for objects:

$foo = new StdClass;
$foo->bar = new StdClass;
$foo->bar->baz = 'qux';

echo "hi $foo->bar->baz mom";    // prints: PHP catchable: Object of StdClass could not be converted to string
echo "hi {$foo->bar->baz} mom";  // prints: hi qux mom

In both cases, the non-braced versions will be parsed/executed as the equivalent of:

echo "hi " . $foo[1]   . "[2] mom";
echo "hi " . $foo->bar . "->baz mom";
Marc B
  • 356,200
  • 43
  • 426
  • 500