Solved

Should you use {} for a variable in heredoc

Posted on 2016-11-06
3
26 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 30

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 51

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
The viewer will learn how to count occurrences of each item in an array.

757 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

27 Experts available now in Live!

Get 1:1 Help Now