Home » Php » php – What does { } do within a string?

php – What does { } do within a string?

Posted by: admin July 12, 2020 Leave a comment

Questions:
$name = "jason";
$p = "hello-{hello2}-$name-{$name}";
echo $p;

output :

hello-{hello2}-jason-jason

Came across some examples of prepared statements and noticed this. If its encompassing a variable, it removes them, otherwise it keeps them. Why is this behavior necessary when

echo "$name";

gets you the same result as

echo "{$name}";

or is it just readability?

How to&Answers:

It’s used as a delimiter for variables in strings. This is necessary in some cases, as PHP’s string parser isn’t Greedy aand will mis-interpret many common structs.

e.g.

$foo = array();
$foo['bar'] = array();
$foo['bar']['baz'] = 'qux';

echo "Hello $foo[bar][baz]";

will actually print

Hello Array[baz]

Because it’s parsed as

echo "Hello ", $foo['bar'], "[baz]";
        ^          ^           ^
      string     array       string

Using {} forces PHP to consider the array reference as single entity:

echo "Hello, {$foo['bar']['baz']}";  // prints "Hello, qux"

It also helps differentiate ambiguous stuff

$foo = 'bar';

echo "$foos" // undefined variable 'foos'
echo "{$foo}s" // variable containing 'bar' + string 's'

Answer:

It’s something called “complex syntax”

From php.net you can go to Complex (curly) syntax section and see many examples.

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 {$.

Functions, method calls, static class variables, and class constants inside {$} work since PHP 5. However, the value accessed will be interpreted as the name of a variable in the scope in which the string is defined. Using single curly braces ({}) will not work for accessing the return values of functions or methods or the values of class constants or static class variables.

Answer:

It does not necessarily generate the same output:

$name = 'foo';
$names = 'bar';

echo "Output1: $names";
echo "Output2: {$name}s";

Output

Output1: bar
Output2: foos

Also you can access complex structures via the curly syntax like {$foo->bar}.

Answer:

This syntax is useful when you want to display variable following some string and you don’t want any space between them.

Compare the following:

<?php

$name='John';

echo "My name is $nameathan. I'm twenty years old<br />";
echo "My name is {$name}athan. I'm twenty years old<br />";

It will give you result:

Notice: Undefined variable: nameathan in ... on line 5
My name is . I'm twenty years old
My name is Johnathan. I'm twenty years old

For first echo it will generate notice and won’t work as expected because PHP doesn’t know that you want to use variable $name in the string and not $nameathan. Using curly braces in second case solves the issue.

Of course you can concatenate string this way:

echo "My name is $name"."athan. I'm twenty years old<br />";

and it also solves the issue but if you have many such variables in string it will be much more convenient to use curly braces.