PHP variables parsing

I am studying the lesson n° 7 called Variable Parsing. The first example used is this
$dog_name = “Tadpole”;
$favorite_food = “salmon”;
$color = “brown”;

echo “I have a $color dog named $dog_name and her favorite food is $favorite_food.”;

While another example is with curly brakets. When should I use “{ }” and when not?

Hello @vanhiro :grinning:

Given the conditions, this is correct.

Curly brackets are used if you need to modify the word slightly. It is explained in the lesson but here is an example:

$word = 'animal';

echo "I have one $word'; 

This will work now, but suppose you have multiple animals. At this point you will need the word animal to be plural.
Instead of changing the variables value, you can then use curly brackets to add onto the variable:

$word = 'animal';

echo "I have two ${word}s;

Without the curls brackets the program would search for a variable name words which does not exist.

3 Likes

Thank you very much.

1 Like

My pleasure! Glad to help!

1 Like