1

What is the functional difference between these two snippets of code

<!--?php include('json-ld.php'); ?--><script type="application/ld+json">// <![CDATA[
<?php echo json_encode($payload); ?>
// ]]></script>

and

<?php include('json-ld.php'); ?><script type="application/ld+json">
<?php echo json_encode($payload); ?>
</script>

My objective is to apply a JSON-LD file (containing schema.org structured data) to my WordPress site. The first code is taken from this page, and purports to do what I need. But I can't understand why the author would post a snippet with so many comments. From what I understand about HTML and JS comments, the code appears to be functionally equivalent to the bottom code. I am posting here to assure myself that I am not misunderstanding something about this syntax.

Perhaps there is a security purpose for using the commented code? If so, I would be interested in practicing best practices in terms of security.

Thanks.

Richard Parnaby-King
  • 14,703
  • 11
  • 69
  • 129
cag8f
  • 817
  • 1
  • 10
  • 33
  • "Curious XXXX syntax" is vary vague and doesn't provide a specific problem. You should change your title. It makes your post super misleading even before reading it. The title screams "down vote me please". Usually, a beginner mistake. I'm sure the down votes are for your title, and the up votes are the people that actually read your post. ;) – kemicofa ghost May 24 '17 at 08:47
  • https://stackoverflow.com/questions/66837/when-is-a-cdata-section-necessary-within-a-script-tag – inarilo May 24 '17 at 08:50
  • @rottenoats OK thanks for that comment. I do indeed know that posts here need to be in the form of a question. But for some reason that knowledge escaped me when creating this particular post. – cag8f May 25 '17 at 06:23

2 Answers2

2

CDATA stands for Character Data and it means that the data in between these strings includes data that could be interpreted as XML markup, but should not be. This is so that html tags and such can be used without it breaking the xml code - each html tag would be represented as a child-node to the xml. By using the CDATA "comment" you are telling the xml to consider the html tags as a string value, not a child-node.

Regarding the first line: <!--?php include('json-ld.php'); ?--> This isn't including the file - this may be a typo on the original authors' part. If your code works without it, it is probably safe to remove; otherwise, fix it ;)

Richard Parnaby-King
  • 14,703
  • 11
  • 69
  • 129
  • thanks for that. Your response made it clear that I should leave in the code: <![CDATA[ ]]. But what about the double forward slash (//) that appears on lines 1 and 3. What is the function of those? As I understand, they are JavaScript comment designators. If so, they appear to be commenting out the <![CDATA[ and ]]. edit: I'm reading through https://stackoverflow.com/questions/66837/when-is-a-cdata-section-necessary-within-a-script-tag now, and they seem to discuss the use of such syntax. – cag8f May 25 '17 at 06:22
  • edit 2: OK I think I'm clear on this--for now at least. – cag8f May 25 '17 at 06:28
1

From what I see, it's purely so that potential output of <?php include('json-ld.php'); ?> are not treated as HTML.

Since most of the time there is no output in <?php include('json-ld.php'); ?> , <!-- <?php include('json-ld.php'); ?> --> just do <!----> . But potential PHP notices or errors that randomly pop will end commented and not breaking the DOM.

Larandar
  • 132
  • 1
  • 10