Solved

Should you use {} for a variable in heredoc

Posted on 2016-11-06
3
52 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 110

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 56

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

749 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