Difference between echo with Brackets and without Brackets

Asked

Viewed 36 times

0

Can you tell me the difference between echo and Brackets?

<?php echo "OI {$nome}"?>

and without Brackets

<?php echo "OI ".$nome;?>
  • https://www.php.net/manual/en/language.types.string.php#language.types.string.Parsing

  • Why is it marked as duplicate? The answer to the question includes 3 items, the question of which this one was cited as duplicated only contains one of these 3 items.

  • 1

    @Ronaldoaraújoalves Which two were missing that are not met by the explanation in the other answer?

  • @Andersoncarloswoss I made some confusion, I looked again at the answer and it’s perfect. My fault.

  • @Ronaldoaraújoalves Opa, I thought I had forgotten something :D

1 answer

2


Response and example taken of this link:

1- echo with Curly Brackets allows you to use the variable inside strings to separate the variable value of the text around it.

$verb = 'add';
echo "Present tense of this verb is $verb";

Imagine you want to add "ed" to the end of the variable without having to reset it:

echo "Past tense of this verb is $verbed";

This will generate an error, PHP will try to access the value of the variable "verbed".

But it would solve if using the following code:

echo "Past tense of this verb is {$verb}ed";

2- If $Verb is an array, an element can be accessed like this:

echo "Past tense of this verb is {$verb['past_tense']}";

3- If $Verb is an object and has a method called getPastTense() which returns a string, can be used this way:

echo "Past tense of this verb is {$verb->getPastTense()}";

Browser other questions tagged

You are not signed in. Login or sign up in order to post.