Question:
By including this method in 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
Answer:
This usually occurs when we forget to close a {
(key, brace, 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
}
?>
Here are a series of reasons that can cause the error "unexpected end":
Missing closing braces:
<?php
if (condição) {
echo 'Olá mundo';
?>
Missing semicolon at the end:
<?php
if (condição){
echo 'Olá mundo'
}
?>
Failure to close the quotation marks or apostrophe before the ;
:
<?php
echo "Olá mundo;
?>
Missing apostrophe:
<?php
echo 'Olá mundo;
?>
Missing 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 the short_open_tag turned off in
php.ini
and it won't always be, it depends on how you used it, test the following example:
<?php
if (true) {
?>
oi
<? /*aqui é um a short_open_tag */
}
?>
In the case of the short_tag note that the first part opens the if with
{
, but how is it inside the short_open_tag<? ... ?>
doesn't run, so PHP doesn't recognize the}
The importance of indentation
Something that can help you remember to close is the indentation . In typography, indentation is the indentation of a text in relation to its margin. In computer science, indentation (indentation, a 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 in order to emphasize 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 braces (keys) and be easy to read to see if any closing braces (braces) were missing.
Note: In a single programming language, there can be several types of indentation, this is a somewhat personal choice, but all still have the same purpose, to make it easier to read.