• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Echoing String Variable In HTML Label

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to echo a string variable in an HTML label. I cannot, for the life of me, figure out why it isn't working. I have tried numerous things to resolve this, but none of them have worked. That said, I know it is suppose to work the way I have it coded, so there's something else I am missing here.



I've also tried using curly braces instead, like this...
 
Bartender
Posts: 15743
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ItDoesntWorkIsUseless. In the future, please tell us what you expected to happen and what actually happened.

You're trying to compose a string and assign it to a variable. You can't use echo for this, because echo is for printing strings to the output, not for composing them.

Using braces to expand your variable won't work, because you've used single quotes around your string. Variable expansion only works in double quoted strings and when using Heredoc syntax.

Heredoc might actually be useful in this case, because you won't have to escape all the  double quotes that you use inside your string. It would look something like this:

A much much much better solution however is to not build up your HTML page in such a piecemeal fashion, but instead create a template for the entire page in a language like Blade or Twig, and then pass the values you want to inject into the template from your PHP controller.
 
Adam Wentz
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for responding!

I've never actually heard of "heredoc" in PHP, but I briefly looked in to it at your mentioning it, and then tried your proposed solution. However, it throws a parse error:

Parse error: syntax error, unexpected '<<' (T_SL) in...




Now, for the record, everything about this concatenation works, except for the integrated variable, which is not displaying its value. So, the rendered output is "Copy entries to Address."
When trying to use heredoc, the whole page breaks and the referenced parse error is thrown.
 
Adam Wentz
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was tinkering with the syntax, and I finally managed to get the heredoc solution to work as follows:

 
Stephan van Hulst
Bartender
Posts: 15743
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason it didn't work is because the closing tag must begin at the start of a new line. You can not have any form of indentation.

It's good you got it to work, but I really strongly recommend using a template language instead stringing together your HTML by hand.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic