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

0

I would like to know what caused this problem that I resolved "sort of without knowing how".

I created the file mysql.php using the PSPad editor to debug some queries and mysteriously started to look like the error:

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

In the code there is nothing to denote 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 in Notepad and pasting the code copied from the file created in PSPad

    
asked by anonymous 17.09.2015 / 04:38

2 answers

2

I've been through this, the solution is simple, but until I find it ... It is as follows:

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

In your case, just 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" menu.

But what is "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 flow.

( link )

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

    
26.01.2017 / 15:00
-1

Apparently the error is being caused by the comment shortly after opening the PHP tag. Try removing the comment and putting the command namespace soon after the <?php .

    
17.09.2015 / 15:49