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

16

When adding this method to 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

    
asked by anonymous 17.02.2015 / 18:29

4 answers

23

This usually occurs when we forget to close a { (key, key, brace, or curly bracket).

In the case of the code, it looks like this:

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

When this is correct:

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

Following are a series of reasons that may cause the "unexpected end" error:

Lack of closing key (keys):

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

Missing semicolon at the end:

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

Failed to close quotation marks or apostrophes before ; :

<?php
    echo "Olá mundo;
?>

Missing apostrophe:

<?php
    echo 'Olá mundo;
?>

Lack of a parenthesis:

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

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

  

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

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

In case of the short_tag note that the first part opens the if with { , but since what is inside short_open_tag <? ... ?> does not execute, then PHP does not recognize }

The importance of indentation

Something that can help not forget to close is the indentation . In typography, indentation is the indentation of a text in relation to its margin. In computing science, indentation is a term applied to the source code of a program to highlight or define the structure of the algorithm.

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

Indentation code:

<?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;
}
?>

You notice the difference, so we can organize the "position" of the keys and have the readability to see if any closing keys were missing.

  

Note: In a single programming language, there may be several types of indentation, this is a rather personal choice, but all have the same purpose, / p>

    
17.02.2015 / 18:29
0

Check the file format. This is how I solved the error

  

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

After suffering a little.

Use a file editor such as Notepad++ , in the toolbar, click the " display all characters " button and also look at the editor's footer on the right side of the file format, DOS/Windows , Unix or Macintosh .

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

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

For Macintosh servers, you only need to have a [ CR ] at the end of the line.

    
23.09.2016 / 01:46
0

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

Note: This is not the case for this question, but it is for future reference.

Short tags:

<?
    echo 'Foobar';
?>

Instead of:

<?php
    echo 'Foobar';
?>

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

    
31.05.2017 / 22:27
0

Dude, if you forget to put the name of the 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 to be a lie that someone can forget the role name. But it happens.

    
23.11.2017 / 01:36