• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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...
 
Saloon Keeper
Posts: 13824
312
  • 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
Saloon Keeper
Posts: 13824
312
  • 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.
 
Pay attention! Tiny ad!
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic