Script returning Parse error: syntax error, Unexpected end of file

Asked

Viewed 68,381 times

22

When including this method to a PHP code

<?php
function meuMetodo($a, $b) {
   $c = $a + $b;
   if ($c > 1) {
      return TRUE;
   } else {
      return $c;
}
?>

This error occurs:

Parse error: syntax error, Unexpected end of file in Z: web project1 lib.php on line 10

4 answers

31


This usually occurs when we forget to close one { (key, key, Brace or Curly Bracket).

In the case of the code, it’s like this:

<?php
function meuMetodo($a, $b) {
   $c = $a + $b;
   if ($c > 1) {
      return TRUE;
   } else {
      return $c;
}
?>

When it is right:

<?php
function meuMetodo($a, $b) {
   $c = $a + $b;
   if ($c > 1) {
      return TRUE;
   } else {
      return $c;
   }//Faltou este
}
?>

Following are a number of reasons that may cause "Unexpected end error":

Missing key (lock braces):

<?php
if (condição) { 
     echo 'Olá mundo'; 
?> 

Missing semicolon at the end:

<?php
if (condição){ 
    echo 'Olá mundo'
} 
?>

Failure to close quotation marks or apostrophe before the ;:

<?php
    echo "Olá mundo;
?>

Apostrophe missing:

<?php
    echo 'Olá mundo;
?>

Missing a parenthesis:

<?php
    metodoChamado(1, 2, 3, 'a', 'b', 'xyz';
?>

Mixing PHP tags (<?php) with short_open_tag (<?):

Of course, this will only occur if the server has the short_open_tag turned off on php.ini and will not always, depends on how you used, test the following example:

<?php
if (true) {
?>
oi
<? /*aqui é um a short_open_tag */
}
?>

In the case of short_tag note that the first part opens the if com {, but like what’s inside short_open_tag <? ... ?> is not executed, so PHP does not recognize the }


The importance of indentation

One thing that can help you remember to close indentation. In typography, indentation is the indentation of a text relative to its margin. In computer science, indentation (indentation, neologism derived from the English word indentation) is a term applied to the source code of a program to emphasize or define the structure of the algorithm.

In most programming languages, indentation is used to highlight the structure of the algorithm, thus increasing the readability of the code.

Code without indentation:

<?php
$a = 1;
if ($a > 0) {
if ($a > 10) {
$a = 0;
}
echo $a;
}
?>

Code with indentation:

<?php
$a = 1;
if ($a > 0) {
    if ($a > 10) {
        $a = 0;
    }
    echo $a;
}
?>

Noticed the difference, so we can organize the "position" of the keys (braces) and have the ease in reading to see if any key (braces) were missing.

Note: In a single programming language, there can be several types of indentation, this is a choice somewhat personal, but all still have the same purpose, to facilitate reading.

1

Another possibility is when using short tags and the server is not configured to accept them.

Note: It is not the case of this question, but it is for future references.

Short tags:

<?
    echo 'Foobar';
?>

Instead of:

<?php
    echo 'Foobar';
?>

Note that the tag that opens the block has php after the interrogation.

  • I tested it now and the error nay occurs if short tags are off short_open_tag=Off, in fact it is interpreted only as normal text. Thank you anyway.

  • 1

    I was able to find exactly how the short_open_tag failure could occur, I edited the answer https://answall.com/posts/51003/revisions

0

Dude, if you forget to put the name of Function gives this error too. For example:
wrong way that generates this error:

function(){
     //vai gerar esse erro como se tivesse faltando chave '}'
}

Right way:

    function nome(){
     //ok 
    }

It seems untrue that one can forget the name of the function. But it happens.

  • Yes too, but forget one ) or } is common, now forget the name of an entire function is far-fetched, unless I accidentally deleted it, I’m still a Closure, probably have the delimiter ; (point and comma).

  • In javascript it is possible to create function without name. It can confuse.

0

Check the file format. That’s how I solved the mistake

"Parse error: syntax error, Unexpected end of file"

after suffering a little.

Use a file editor like Notepad++, in the toolbar, click the button "display all characters" and also note in the editor footer on the right side the file format, DOS/Windows, Unix or Macintosh.

For servers in environments UNIX (Linux), at the end of each row of the file should have a [LF] only. [CR][LF] also work, but these are from the DOS/Windows environment.

For Windows servers (IIS for example) have to have a [CR][LF] at the end of the line.

For Macintosh servers have to have only one [CR] at the end of the line.

  • Mac does not use [CR] That was just until the Mac OS 9 and it is no longer used since 2002, the Mac core has become like-Unix, and uses [LF]. About scripts in PHP is indifferent to use [CR]+[LF] or [LF], PHP interpreter simply ignores this, the only difference is to use inside echo, var_dump, print, print_r, but only affects the output, the interpretation and execution of the script feel no difference, what you should have was a "NULL" character or a [BS].

Browser other questions tagged

You are not signed in. Login or sign up in order to post.