Solved

Should you use {} for a variable in heredoc

Posted on 2016-11-06
3
35 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 108

Accepted Solution

by:
Ray Paseur earned 350 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 150 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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now