computerbarry — 2013-07-10T15:58:46-04:00 — #1
I have a small issue trying to add php code within html code within a php variable. If that makes sense.
Just can't get this to work. Does anybody know how to add the correct brackets or what I need?
I've broken this down for easy viewing.
$breadcrumbs = '<li>
<a href="http://www.example.com/venue/<?= $row['venue_name'] ?>">
<span><?= $row['venue_id'] ?></span>
Many thanks, Barry
fretburner — 2013-07-10T16:07:07-04:00 — #2
You don't need to include the php tags. If you use double-quotes PHP will parse any variables it finds within the string. When using associative arrays, you either need to leave off the quotes around the key, or enclose them with braces:
$breadcrumbs = "<li>
computerbarry — 2013-07-10T16:17:40-04:00 — #3
Cool. Thanks seems so simple now
The problem I'm having is I need to use single quotes as I have a lot of markup/attributes wrapped up within this and many other variables using double quotes.
Wouldn't that mean I'd need to escape every double quote to avoid any errors? That was the reason to use double quotes.
fretburner — 2013-07-10T16:35:07-04:00 — #4
You could use single quotes around your html attributes, like I've done in the example above. The other option is to use string concatenation:
$breadcrumbs = '<li>
computerbarry — 2013-07-10T16:59:36-04:00 — #5
So many ways. Always good to know thanks.
Seems to be working now well no errors but the values within the link and span are empty.
Have we missed something? Do we need to echo them?
fretburner — 2013-07-10T17:07:51-04:00 — #6
Hmm.. no you don't need to echo them. It seems more likely that those values aren't set for some reason. You could try:
and see what you get.
computerbarry — 2013-07-10T18:04:59-04:00 — #7
I had the snippet above the SQL so the row had no value.
And just one question. You mention:
When using associative arrays, you either need to leave off the quotes around the key...
What do you mean when you say key? What are you referring to?
I keep hearing this time and time again but not sure what it is even though I'm probably using it all the time.
fretburner — 2013-07-10T18:16:34-04:00 — #8
Key is just the name given to the 'label' that is used to reference a value in an array. In the case of associative arrays like you're using in your example, the key is a string, but it can also be a number as in the case of indexed arrays. With your array
$row, the string
venue_name is an array key.
computerbarry — 2013-07-10T19:05:57-04:00 — #9
That makes a bit more sense now.
I appreciate your time and information fretburner, thank you
qmonkey — 2013-07-10T23:23:20-04:00 — #10
I'll add another option: heredoc which is useful if you have a large block of text or a lot of mixed quotes.