Solved

Should you use {} for a variable in heredoc

Posted on 2016-11-06
3
41 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 109

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 54

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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…

813 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

15 Experts available now in Live!

Get 1:1 Help Now