1

The variable $a below does not seem to parse properly when if I attempt to declare it using a heredoc. It however, does work when I define it with the simple = declaration method. I would like to be able to define it as a heredoc, because a lot of HTML code must be called all at once, and in this example I have just simplified it for demonstrative purposes.


$a = "<a href = \'http://www.google.com\'>Google</a>";

echo "
  <div id = \"test\" 
      ondblclick = \"document.getElementById('test').innerHTML = '$a';\">
        Change Event
  </div>
"; 

// when the user clicks the text "Change Event", 
// it should turn into whatever $a is, in this case a link to Google

The previous code works, however when I try to convert it to a heredoc, something does not parse correctly.


I have tried using four kinds of quote styles, ", \", ', and \'

\' is the quote style required by where the variable lies in the code, within JavaScript, as the other three styles have already been used, and it is the quote that works (obviously) when I am declaring a variable in a simple (=) way.

When I use " or \" the code yields:

Screenshot

When I use ' or \' the code yields:

Screenshot2

In both cases, the ondblclick functionality does not work either. I am not sure what is going wrong and why heredoc is not parsing the way it seems like it should.


Here is one of my attempt at making the heredoc; the only difference between my attempts are the quote styles around the link.

$a = <<<EOT

    <a href = \'http://www.google.com\'>Google</a>

EOT;

// as stated above, I have tried ", \", ', and \'
7
  • 1
    It's highly not recommended to have spaces between the attribute (href) and the value or the equal sign. <a href="url"> and not <a href = "url"> Commented Oct 24, 2011 at 18:28
  • 1
    It's highly likely that the manual already addresses all your concerns: php.net/manual/en/… Commented Oct 24, 2011 at 18:30
  • @Truth Thanks, I'll clean that up Commented Oct 24, 2011 at 18:30
  • 2
    @hakre to be perfectly honest with you, no one uses HTML 2.0 anymore. And that still doesn't make it recommended. Commented Oct 24, 2011 at 18:34
  • 2
    @Truth It's sad that there is no way to downwote such a pointless comment like yours. Commented Oct 24, 2011 at 19:31

4 Answers 4

2

Hmmm trying to think of a way around this whilst still using heredoc... the double quotes within .innerHTML must be escaped when printed to the browser, or you get the problem you have. Some browsers will be kind and automatically escape them but on the whole they won't and, if truth be told, they shouldn't.

The only way I can get this code to work is by removing the "" from the href attribute completely.

It's really not recommended to have .innerHTML used this way as the <a> tag now contains technically invlalid HTML, but it seems to work in Firefox,IE 9 (quirks mode on and off) and Chrome.

$a = <<<EOT
<a href=http://www.google.com>Google</a>
EOT;
$b = <<<EOT
<div id="test" ondblclick="document.getElementById('test').innerHTML = '$a';">Change Event</div>
EOT;
echo $b;
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, thank you. This is the first answer validly addressing the problem. I have thought of this as well, my concern is having bad code as a workaround. Although, this may be the only way.
I have a nasty feeling that it is the only way using heredoc that will work when trying to output a double quoted string within an already double quoted string. One thing is for sure, it is definitely invalid HTML and could / will lead to problems in older browsers. :(
1

Don't escape your quotes. Do something like this:

<?php

$foo = "teststring";
$bar = <<<EOT
<a href="http://www.google.com/?q={$foo}">Click here</a>
EOT;

?>

1 Comment

This isn't working because PHP is parsing between " and ", therefore the first " after a href= is causing the echo function to be terminated; which was initiated with a " also.
1

In PHP you don't need to use echo to output a string.
Just close PHP tag and write whatever you want AS IS, without any single syntax restriction (as long as you follow THML standard of course), opening PHP only when needed.

?>
  <div id = "test"  
      ondblclick = "document.getElementById('test').innerHTML = '<?=$a>';"> 
        Change Event 
  </div> 

then inspect resulting HTML source if it fits your expectations.
That's all.

if you still experiencing problems with your code - well, it's not PHP fault anymore. Fix your dhtml syntax.

(notice the color highlighting which becoming possible when you are writing (D)HTML as is, without ugly PHP echos)

2 Comments

Wow, this is genius. Let me take a look at this for a while.
the only possible issue is a shortland I used, <?=$a>. If it won't work, either change it to <?php echo $a?> or turn short_oprn_tag PHP setting on
0

This works for me:

<?php
$a = <<<EOT
<a href="http://www.google.com">Google</a>
EOT;
echo "
  <div id=\"test\" 
      ondblclick=\"document.getElementById('test').innerHTML = '$a';\">
        Change Event
  </div>
";

So does this:

<?php
$a = <<<EOT
<a href="http://www.google.com">Google</a>
EOT;
$b = <<<EOT
<div id="test" ondblclick="document.getElementById('test').innerHTML = '$a';">Change Event</div>
EOT;
echo $b;

See this codepad for output.

5 Comments

I am seeing that it works on codepad output, however when I copy those exact lines to my site, I am getting the errors I described above. PHP Version 5.3.5-1ubuntu7.3
Then it's probably not the code that is at fault, but your setup. Can you link to the site where this occurs?
Based on your codepad output, it still appears to have parsing errors that I am describing upon closer examination, as there is still no 1-1 mapping of quote-styles.
@DươngVăn Sorry, I did not understand this.
I am saying the area between the double quotes is not what you or I intended. PHP is interpreting it just fine, even when I copy the output of your codepad into HTML format, it still gives the exact same error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.