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.
<?php namespace MyApp\Utils; function greet() { return "Hello"; }
| Step | Code Line | Action | Namespace Context | Result |
|---|---|---|---|---|
| 1 | <?php | Start PHP code | Global | PHP interpreter starts reading |
| 2 | namespace MyApp\Utils; | Declare namespace | MyApp\Utils | Namespace set to MyApp\Utils |
| 3 | function greet() { return "Hello"; } | Define function | MyApp\Utils | Function greet() defined inside namespace |
| 4 | ?> | End PHP code | MyApp\Utils | PHP interpreter stops reading |
| Variable | Start | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|
| Namespace | Global | MyApp\Utils | MyApp\Utils | MyApp\Utils |
| Function greet | Not defined | Not defined | Defined in MyApp\Utils | Defined in MyApp\Utils |
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