-1

i was wondering what the difference is when u put a variable in another variable with the notation. So i have a variable "Body" and in there is HTML tags, text and PHP variables, ive found out i can put them in between hooks but that they would react exactly the same. Example:

In this piece of code the variable is in between { } hooks.

$body = "
             <table style='border: 1px;'>
             <tr>
             <td><b>Naam:</b></td><td>{$naam}</td><br>
             </tr>
             </table>"

And here it is not.

$body = "
             <table style='border: 1px;'>
             <tr>
             <td><b>Naam:</b></td><td>$naam</td><br>
             </tr>
             </table>"

And this both reacts exactly the same. So can anyone tell me if this has an actual use, or that this is just like all PHP with the 10 ways to do the same thing.

Thanks in advance.

Addition

This is not a duplicate ofThis. It does not explain the part of why a variable inside of a variable can be put in between curly brackets.

Community
  • 1
  • 1
B. Dionys
  • 842
  • 6
  • 30
  • 1
    It's of use with e.g. array / object notation for interpolation. – Jonnix Apr 26 '17 at 12:39
  • 3
    Using brackets also makes it more readable (as to my opinion). Also be aware inline variables only work when the string is double quoted. – Niek van der Maaden Apr 26 '17 at 12:39
  • Yes it does make it mor readable that is for sure @Niek van der Maanden. – B. Dionys Apr 26 '17 at 12:43
  • @Pharaoh It does not explain this exact problem there. it has a lot of solutions but it does not say a thing about the brackets inline variables – B. Dionys Apr 26 '17 at 12:44
  • 1
    See the answer here @H.Brendan http://stackoverflow.com/questions/2596837/curly-braces-in-string-in-php , Very good explanation ;) – Niek van der Maaden Apr 26 '17 at 12:46
  • 1
    _“It does not explain the part of why a variable inside of a variable can be put in between curly brackets”_ – http://php.net/manual/en/language.types.string.php#language.types.string.parsing explains it. – CBroe Apr 26 '17 at 12:49
  • @NiekvanderMaaden Ah yes I see. Thank you :D. can u put your comment into an answer? Then I will click the hook for the support. Or should I delete the question becouse this may or may not be seen as an duplicate – B. Dionys Apr 26 '17 at 12:50
  • 1
    Ive added it as an answer, its up to your if you want to delete it. – Niek van der Maaden Apr 26 '17 at 12:51
  • @CBroe Also thank you for the answer this did explain it also. – B. Dionys Apr 26 '17 at 12:53

1 Answers1

1

Usage of curly braces inside " is particularly useful when you want to add complex instruction or access object/array properties.

This code will perfectly work

<?php echo "Test {$foo['bar']}"; ?>
<?php echo "Test {$foo->bar}"; ?>

While this one will fail

<?php echo "Test $foo['bar']"; ?>
<?php echo "Test $foo->bar"; ?>

So yes, it's not particularly useful if you are accessing a simple variable, but when you want to play with array and object, it's useful.

LoïcR
  • 4,595
  • 1
  • 28
  • 45