Take the 2-minute tour ×

I was running the following PHP code:


There were no parse errors and the output was "?>" (example).

In similar cases I do get a parse error:


Parse error: syntax error, unexpected '<' in ...

Why doesn't <?php </script> ?> give the same error?

share|improve this question
Just curious. What were trying to achieve or what was it that you were experimenting on? –  asprin Nov 5 '12 at 8:22
Actually the case was different last night I missed the php closing tag.And after this I had a </script> tag and I spent more than 30 min to figure out why this does not have any error buts still the output is not as desire. –  Irfan Nov 5 '12 at 8:25
I don't want to live on this planet any more. –  Kzqai May 6 '13 at 16:48
PHP does that to people. –  xiankai Sep 29 '13 at 11:12

2 Answers 2

up vote 261 down vote accepted

This must be because there are various ways of starting a block of PHP code:

  • <? ... ?> (known as short_open_tag)

  • <?php ... ?> (the standard really)

  • <script language="php"> ... </script> (not recommended)

  • <% ... %> (deprecated and removed ASP-style tag after 5.3.0)

Apparently, you can open a PHP block one way, and close it the other. Didn't know that.

So in your code, you opened the block using <? but PHP recognizes </script> as the closer. What happened was:

<?php       <----- START PHP
</script>   <----- END PHP
?>          <----- JUST GARBAGE IN THE HTML
share|improve this answer
Note that using <script language="php"> is not recommended. –  Jan Dvorak Nov 5 '12 at 8:24
Yeah even <script language="php">echo "lol" ; ?> is valid PHP code .... –  Baba Nov 5 '12 at 8:26
Quick note, because I remembered there was some change with the short_open_tag directive. In PHP 5.4 <?= short for <?php echo no longer requires the short_open_tag. Going between systems where <?= was available and not available always frustrated me. php.net/manual/en/ini.core.php#ini.short-open-tag –  Chris Nov 5 '12 at 12:11
@thatmiddleway Note that the official document recommends <?php ... ?> as well: php.net/manual/en/language.basic-syntax.phpmode.php –  Jan Dvorak Nov 6 '12 at 19:06
Moreso than the 100+ upvotes, I'm shocked by the one downvote. WTH? –  Trott Nov 8 '12 at 4:38

In PHP, you can use the script tag to open a PHP block.

So you can use

<script language="php">
    echo 'hello world';

So in your example you have mixed the normal open tag, <?php, with the closing tag, </script>. So the parser assumes that all the text after the closing script tag is normal HTML.

Read more in Escaping from HTML.

share|improve this answer

Your Answer


