Namespace declaration statement has to be the very first statement or after any declare call in the script

Asked

Viewed 5,839 times

1

I wonder what caused this problem that I solved "kind of without knowing how".

I created the file mysql.php using Pspad editor to debug some queryes and mysteriously started to look like the bug:

Fatal error: Namespace declaration statement has to be the very first statement in the script in C:\localhost\teste\libraries\teste\mysql.php on line 2

Since in the code there is nothing that denotes such an error:

<?php
namespace MysqlTESTE;  //utilização de namespaces (CRUD)

include 'server.php'; 
use Mysqli; 
...

I solved the problem by creating a new file mysql.php on Notepad and pasting on it the code copied from the file created on Pspad

3 answers

8


I’ve been there, the solution is simple, but until I find her... Is the following:

Some text editors in UTF-8 environments add BOM at the beginning of text files.

In your case it would be enough to change the editor to use UTF-8 (No BOM) if he gave you this option. In Notepad++ for example, you find this and other options in the "Format".

But what is the "GOOD"?

The byte order mark (BOM) is a Unicode character used to denote the end (byte order) of a text file or data stream, with U+FEFF code. Its use is optional and, if used, should appear at the beginning of the text stream.

(https://en.wikipedia.org/wiki/Byte_order_mark)

I remember that if I opened the file in netbeans, I could see the  character at the beginning of the sheet

  • Really I only managed to solve this problem by changing editor .. in the case I used the "Notepad" of Windos. But at the time I didn’t understand what caused this problem.

-1

Apparently the error is being caused by the comment just after opening the PHP tag.
Try to remove the comment and put the command namespace shortly after the <?php.

  • Have you tested? Theoretically it doesn’t affect you at all, but that’s what the error says (the namespace declaration should be the first entry in the script).

  • Yes I tested taking the comment and the error continued.

-2

Some cases I already picked was just space at the beginning of the example file.

Repare no começo do arquivo tem um espaço.

By removing this space at the beginning of the file before the php abetura tag the problem has been solved.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.