1

Get this error:

Namespace declaration statement has to be the very first statement in the script

On very start of this code:

namespace app\controllers;
use yii\web\Controller;
use app\models\users;

class UserController extends Controller
{
     public function actionIndex()
     {
         echo "working on .....";
     }
}

2 Answers 2

10

You probably have this error because you have a blankspace after <?php

 <?php
namespace app\controllers;
use yii\web\Controller;
use app\models\users;

class UserController extends Controller
{
     public function actionIndex()
     {
         echo "working on .....";
     }
}

Just delete it

<?php
namespace app\controllers;
use yii\web\Controller;
use app\models\users;

class UserController extends Controller
{
     public function actionIndex()
     {
         echo "working on .....";
     }
}
3
  • Sageth must have meant "before" <?php Aug 22, 2016 at 14:04
  • 5
    Attention, one may also have these kinds of messages if their file starts with UTF8 BOM. e.g. Ultra-Edit default settings for UTF8 new files... In which case you must remove the BOM. See your editor doco. Aug 22, 2016 at 14:06
  • 3
    Yes, this was UTF8 encoding problem I have saved with UTF8 and its work for me.
    – Manish
    May 29, 2017 at 11:13
-1

If don't have namespaces also you will get error. to solve this delete current php file and recreate it. It will solve the problem.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.