not show a parse error?", "text": "<p>I was running the following PHP code:</p> <pre class="prettyprint"><code>&lt;?php &lt;/script&gt; ?&gt; </code></pre> <p>There were no parse errors and the <strong>output was</strong> "<code>?&gt;</code>" (example).</p> <p>In similar cases I do get a parse error:</p> <pre class="prettyprint"><code>&lt;?php &lt;/div&gt; ?&gt; </code></pre> <blockquote> <p>Parse error: syntax error, unexpected '&lt;' in ...</p> </blockquote> <p>Why doesn't <code>&lt;?php &lt;/script&gt; ?&gt;</code> give the same error?</p>", "answerCount": 1, "upvoteCount": 972, "dateCreated": "2012-11-05 08:20:50", "dateModified": "2022-09-18 05:01:31", "author": { "type": "Person", "name": "Irfan" }, "acceptedAnswer": { "@type": "Answer", "text": "<p>This must be because there are various ways of starting a block of PHP code:</p> <ul> <li><p><code>&lt;? ... ?&gt;</code> (known as <code>short_open_tag</code>)</p></li> <li><p><code>&lt;?php ... ?&gt;</code> (the standard really)</p></li> <li><p><code>&lt;script language="php"&gt; ... &lt;/script&gt;</code> (not recommended)</p></li> <li><p><code>&lt;% ... %&gt;</code> (deprecated and removed ASP-style tag after 5.3.0)</p></li> </ul> <p>Apparently, you can open a PHP block one way, and close it the other. Didn't know that.</p> <p>So in your code, you opened the block using <code>&lt;?</code> but PHP recognizes <code>&lt;/script&gt;</code> as the closer. What happened was:</p> <pre class="prettyprint"><code>&lt;?php &lt;----- START PHP &lt;/script&gt; &lt;----- END PHP ?&gt; &lt;----- JUST GARBAGE IN THE HTML </code></pre>", "upvoteCount": 115, "url": "https://exchangetuts.com/in-php-why-does-script-not-show-a-parse-error-1639480263811997#answer-1646667291596488", "dateCreated": "2022-09-17 05:01:31", "dateModified": "2022-09-18 05:01:31", "author": { "type": "Person", "name": "Pekka" } } } }
Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In PHP, why does </script> not show a parse error?

I was running the following PHP code:

<?php      </script> ?> 

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

In similar cases I do get a parse error:

<?php      </div> ?> 

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

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

like image 972
Irfan Avatar asked Nov 05 '12 08:11

Irfan


People also ask

How can solve parse error in PHP?

To solve the missing parenthesis error in PHP, the code has to be checked from the beginning to search for it. One way to avoid errors is to use proper indentation in the code. Once all the parentheses in the code have been set correctly, parse error: syntax error, unexpected $end will be fixed.

How do I fix parse error syntax error unexpected?

The best way to solve it is to remove the recently added plugins by disabling them. The WordPress site is also likely to generate an error after a code edit. A mistake as simple as a missing comma is enough to disrupt the function of a website.

What does Unexpected end of file mean in PHP?

The “unexpected end of file” error is not specific to WordPress, and can happen on any PHP-based website. This specific error means the file mentioned in the error message ends abruptly without the proper closing tags, and the code was unable to be parsed as a result.


1 Answers

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 
like image 115
Pekka Avatar answered Sep 18 '22 05:09

Pekka