?
Solved

Should you use {} for a variable in heredoc

Posted on 2016-11-06
3
Medium Priority
?
67 Views
Last Modified: 2016-11-07
I have just felt the need to use heredoc to display multiple products from the database as it seems easier than trying to fiddle around with "" and ''. But like everything else, I am new to it and just wanted to know if I should use curly braces or not? I tried both ways and it didn't make a difference to what I saw outputted but I saw an example where someone said you should. I just wanted to know why. e.g.:

-$discount%

Open in new window


vs

-{$discount}%

Open in new window

0
Comment
Question by:Black Sulfur
3 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1400 total points
ID: 41876095
You need those curly braces when you have quoted array indexes.  If you use object notation, you don't need the curly braces.  Try a couple of things like this and see what you think.  I prefer the object notation, and there are a whole host of other reasons, too advanced to go into now, to use objects instead of arrays whenever possible.
$doc = <<<EOD
Here is the price: $row['price']
EOD;

Open in new window

$doc = <<<EOD
Here is the price: {$row['price']}
EOD;

Open in new window

$doc = <<<EOD
Here is the price: $row->price
EOD;

Open in new window

0
 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 600 total points
ID: 41876097
No. Curly braces are only required if you use a variable with array notation. For instance
echo <<<HDC
bla bla bla -$discount% bla bla bla {$user['last_name']} bla bla bla

Open in new window

works perfectly :)
0
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 41876713
As a side note  I prefer to use {} around all variables - it is a bit of extra typing but it does two things

In my editor I get context colouring on the variable - makes strings easier to read with respect to where variables are.

I think it looks neater in terms of making the variables stand out.

Just my personal preference though.
1

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question