Bird
Raised Fist0
PHPprogramming~10 mins

Namespace declaration syntax in PHP - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Namespace declaration syntax
Start PHP file
↓
Declare namespace
↓
Write code inside namespace
↓
Use code with namespace
↓
End of file
This flow shows how a PHP file starts, declares a namespace, contains code inside it, and ends.
Execution Sample
PHP
<?php
namespace MyApp\Utils;
function greet() { return "Hello"; }
This code declares a namespace and defines a function inside it.
Execution Table
StepCode LineActionNamespace ContextResult
1<?phpStart PHP codeGlobalPHP interpreter starts reading
2namespace MyApp\Utils;Declare namespaceMyApp\UtilsNamespace set to MyApp\Utils
3function greet() { return "Hello"; }Define functionMyApp\UtilsFunction greet() defined inside namespace
4?>End PHP codeMyApp\UtilsPHP interpreter stops reading
💡 Reached end of PHP file
Variable Tracker
VariableStartAfter Step 2After Step 3Final
NamespaceGlobalMyApp\UtilsMyApp\UtilsMyApp\Utils
Function greetNot definedNot definedDefined in MyApp\UtilsDefined in MyApp\Utils
Key Moments - 2 Insights
Why do we write 'namespace MyApp\\Utils;' at the top?
Because the execution_table row 2 shows that declaring the namespace sets the context for all code below it.
Is the function greet() available globally after declaration?
No, as shown in execution_table row 3, greet() is defined inside the MyApp\\Utils namespace, not global.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the namespace context after step 2?
AGlobal
BMyApp\Utils
CUndefined
DMyApp
💡 Hint
Check the 'Namespace Context' column in execution_table row 2
At which step is the function greet() defined inside the namespace?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column in execution_table row 3
If we remove the namespace declaration line, what would be the namespace context at step 3?
AGlobal
BMyApp\Utils
CUndefined
DError
💡 Hint
Without namespace declaration, code stays in global namespace as per variable_tracker
Concept Snapshot
PHP Namespace Declaration Syntax:
- Use 'namespace Name;' at the top of PHP file
- Declares a namespace for all code below
- Functions/classes inside belong to that namespace
- Helps avoid name conflicts
- Must be declared before any code except declare or strict types
Full Transcript
This visual execution shows how PHP reads a file starting with <?php, then encounters a namespace declaration 'namespace MyApp\Utils;'. This sets the namespace context for all following code. Next, a function greet() is defined inside this namespace. The execution table tracks each step, showing the namespace context changing from global to MyApp\Utils at step 2. The variable tracker confirms the namespace and function definition states. Key moments clarify why the namespace declaration is at the top and that functions inside are not global. The quiz tests understanding of namespace context and function location. The snapshot summarizes the syntax and purpose of namespace declarations in PHP.

Practice

(1/5)
1. What is the correct way to declare a namespace in a PHP file?
easy
A. use MyApp;
B. namespace MyApp;
C. package MyApp;
D. module MyApp;

Solution

  1. Step 1: Understand PHP namespace declaration

    PHP uses the keyword namespace to declare namespaces at the top of a file.
  2. Step 2: Identify the correct syntax

    The correct syntax is namespace MyApp; with a semicolon at the end.
  3. Final Answer:

    namespace MyApp; -> Option B
  4. Quick Check:

    Keyword 'namespace' declares namespaces [OK]
Hint: Use 'namespace' keyword followed by name and semicolon [OK]
Common Mistakes:
  • Using 'package' instead of 'namespace'
  • Forgetting the semicolon at the end
  • Using 'use' to declare namespace
  • Writing 'module' which is not valid in PHP
2. Which of the following is the correct syntax to declare a nested namespace in PHP?
easy
A. namespace MyApp/Utils;
B. namespace MyApp-Utils;
C. namespace MyApp.Utils;
D. namespace MyApp\Utils;

Solution

  1. Step 1: Recognize namespace separator in PHP

    PHP uses backslashes (\) to separate nested namespaces.
  2. Step 2: Check each option for correct separator

    Only namespace MyApp\Utils; uses backslashes correctly: namespace MyApp\Utils;.
  3. Final Answer:

    namespace MyApp\Utils; -> Option D
  4. Quick Check:

    Backslash separates nested namespaces [OK]
Hint: Use double backslash \ for nested namespaces [OK]
Common Mistakes:
  • Using forward slash / instead of backslash
  • Using dot . or dash - which are invalid
  • Omitting the semicolon at the end
3. What will be the output of the following PHP code?
<?php
namespace App\Tools;
class Helper {
  public static function greet() {
    return 'Hello from Helper';
  }
}

use App\Tools\Helper;
echo Helper::greet();
?>
medium
A. Hello from Helper
B. Fatal error: Class 'Helper' not found
C. Hello from App\Tools\Helper
D. Syntax error

Solution

  1. Step 1: Understand namespace and use statement

    The class Helper is declared inside namespace App\Tools. The use statement imports it so we can call Helper directly.
  2. Step 2: Check method call and output

    Calling Helper::greet() returns the string 'Hello from Helper'.
  3. Final Answer:

    Hello from Helper -> Option A
  4. Quick Check:

    Use imports class, method returns greeting [OK]
Hint: Use imports namespace class to call methods directly [OK]
Common Mistakes:
  • Forgetting to use the 'use' statement
  • Calling class without full namespace
  • Assuming output includes namespace name
  • Syntax errors from missing semicolons
4. Identify the error in this PHP namespace declaration:
<?php
namespace MyApp\Utils
class Logger {}
?>
medium
A. Class name Logger is invalid
B. Namespace name cannot contain backslashes
C. Missing semicolon after namespace declaration
D. Namespace must be declared after class

Solution

  1. Step 1: Check namespace declaration syntax

    The namespace declaration must end with a semicolon (;).
  2. Step 2: Identify missing semicolon

    The code misses the semicolon after namespace MyApp\Utils.
  3. Final Answer:

    Missing semicolon after namespace declaration -> Option C
  4. Quick Check:

    Namespace declarations require semicolon [OK]
Hint: Always end namespace line with semicolon [OK]
Common Mistakes:
  • Omitting semicolon after namespace
  • Thinking backslash is invalid in namespace
  • Placing namespace after class
  • Misnaming classes
5. You want to declare two nested namespaces Company\Project\Module in one PHP file. Which is the correct way to do this?
hard
A. namespace Company\Project\Module; class Feature {}
B. namespace Company; namespace Project; namespace Module; class Feature {}
C. namespace Company/Project/Module; class Feature {}
D. namespace Company.Project.Module; class Feature {}

Solution

  1. Step 1: Understand nested namespace declaration

    PHP allows declaring nested namespaces in one line using backslashes.
  2. Step 2: Evaluate each option

    namespace Company\Project\Module; class Feature {} correctly declares nested namespaces with backslashes and a semicolon. Separate declarations like namespace Company; namespace Project; namespace Module; class Feature {} place the class in top-level Module, not nested under Company\Project. Using forward slashes or dots as separators is invalid.
  3. Final Answer:

    namespace Company\Project\Module; class Feature {} -> Option A
  4. Quick Check:

    Nested namespaces use backslashes in one declaration [OK]
Hint: Use one namespace line with backslashes for nesting [OK]
Common Mistakes:
  • Declaring multiple namespaces in one file incorrectly
  • Using slashes or dots instead of backslashes
  • Forgetting semicolon after namespace
  • Trying to declare nested namespaces on separate lines
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard