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
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>
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.
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>
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?
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.
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.
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.
That makes a bit more sense now.
I appreciate your time and information fretburner, thank you
I'll add another option: heredoc which is useful if you have a large block of text or a lot of mixed quotes.
This topic is now closed. New replies are no longer allowed.