0

Sometimes I see question mark tags in html, like this.

<? /* JavaScript code goes here*/ ?>

The browser turns these question mark tags into html comments, like this.

// javascript

console.log(document.querySelector("div").innerHTML);
<!--html-->

<div>
  <? console.log("this is javascript") ?>
</div>

Editors like vscode even syntax highlight the code inside these <? tags. But let's say I have code inside these tags like this.

x.map(function(item){return + item + '<li></li>'}).join('')

This is how it renders:

// javascript

console.log(document.querySelector("div").innerHTML);
<!--html-->

<div>
  <?
    x.map(function(item) {
      return + item + '<li></li>'
    }).join("");
  ?>
</div>

So how do these question mark tags work? Is it even valid HTML?

Caleb Liu
  • 627
  • 5
  • 18
  • This is php Are you using PHP as your servercode? https://stackoverflow.com/questions/200640/are-php-short-tags-acceptable-to-use#:~:text=There%20are%20four%20different%20pairs,can%20be%20used%20in%20PHP. – Glycerine Sep 11 '22 at 13:11
  • Looks like PHP being rendered on the page by mistake - perhaps you need to [enable short opening tags in PHP.ini](https://stackoverflow.com/questions/2185320/how-to-enable-php-short-tags) – mplungjan Sep 11 '22 at 13:12
  • And this is the browser attempting to close the start angle bracket: ` x.map(function(item) { return + item + '
  • ` <<< – mplungjan Sep 11 '22 at 13:13
  • This is not php: this is raw HTML – Caleb Liu Sep 11 '22 at 13:14
  • That is PHP syntax, and doesn't belong in client-side HTML in your browser. – Sean Sep 11 '22 at 13:23
  • It's called a "bogus comment" by the HTML parser. It's not valid. – Alohci Sep 12 '22 at 02:09