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.
Very complete. Good
– Miguel
@Miguel thanks!
– Guilherme Nascimento