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>