Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse error: Syntax error, unexpected end of file in my PHP code

People also ask

What is syntax error unexpected end of file?

An Unexpected end of file error in a Bash script usually occurs when you there is a mismatched structure somewhere in the script. If you forget to close your quotes, or you forget to terminate an if statement, while loop, etc, then you will run into the error when you try to execute your Bash script.

What is parse error syntax error unexpected?

A parse error: syntax error, unexpected appears when the PHP interpreter detects a missing element. Most of the time, it is caused by a missing curly bracket “}”. To solve this, it will require you to scan the entire file to find the source of the error.


You should avoid this (at the end of your code):

{?>

and this:

<?php}

You shouldn't put brackets directly close to the open/close php tag, but separate it with a space:

{ ?>
<?php {

also avoid <? and use <?php


I had the same error, but I had it fixed by modifying the php.ini file.

Find your php.ini file see Dude, where's my php.ini?

then open it with your favorite editor.

Look for a short_open_tag property, and apply the following change:

; short_open_tag = Off ; previous value
short_open_tag = On ; new value

I had the same error, but I had it fixed by modifying the php.ini and / or editing the PHP file!

There are two different methods to get around the parse error syntax.

Method 1 (Your PHP file)

Avoid in your PHP file this:

<? } ?>

Make sure you put it like this

<?php ?>

Your code contains <? ?>

NOTE: The missing php after <?!

Method 2 (php.ini file)

There is also a simple way to solve your problem. Search for the short_open_tag property value (Use in your text editor with Ctrl + F!), and apply the following change:

; short_open_tag = Off

to

short_open_tag = On

According to the description of core php.ini directives, short_open_tag allows you to use the short open tag (<?) although this might cause issues when used with xml (<?xml will not work when this is enabled)!

NOTE: Reload your Server (like for example: Apache) and reload your PHP webpage in your browser.


Just go to php.ini then find short_open_tag= Off set to short_open_tag= On


Also, watch out for heredoc closing identifiers.

Invalid Example:

// it's not working!!!

function findAll() {
    $query=<<<SQL
        SELECT * FROM `table_1`;
    SQL; // <-------- THIS IS BAD

    // ...
}

This will throw an exception that resembles the following:

<br />
<b>Parse error</b>:  syntax error, unexpected end of file in <b>[...][...]</b> on line <b>5</b><br />

where number 5 might be the last line number of your file.

According to php manual:

Warning It is very important to note that the line with the closing identifier must contain no other characters, except a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system. This is \n on UNIX systems, including macOS. The closing delimiter must also be followed by a newline.

TLDR: Closing identifiers should NOT be indented.

Valid Example:

function findAll() {
    $query=<<<SQL
        SELECT * FROM `table_1`;
SQL;
    // closing identifier should not be indented, although it might look ugly

    // ...
}

Look for any loops or statements are left unclosed.

I had ran into this trouble when I left a php foreach: tag unclosed.

<?php foreach($many as $one): ?>

Closing it using the following solved the syntax error: unexpected end of file

<?php endforeach; ?>

Hope it helps someone