A namespace in PHP helps organize code by grouping related classes, functions, and constants to avoid name conflicts.
Click to reveal answer
beginner
How do you declare a namespace in PHP?
Use the namespace keyword followed by the namespace name and a semicolon. Example: namespace MyApp\Utils;
Click to reveal answer
intermediate
Can you declare multiple namespaces in one PHP file?
Yes, but only if you use the bracketed syntax for each namespace block. Example: namespace A { /* code */ } namespace B { /* code */ }
Click to reveal answer
intermediate
What happens if you declare a namespace after some code in a PHP file?
The namespace declaration must be the first statement in the file (except for declare statements or comments). Declaring it after code causes a syntax error.
Click to reveal answer
intermediate
Explain the difference between bracketed and unbracketed namespace syntax in PHP.
Unbracketed syntax declares a namespace for the whole file with a semicolon: namespace MyApp; Bracketed syntax wraps code in braces to allow multiple namespaces in one file: namespace MyApp { /* code */ }
Click to reveal answer
Which keyword is used to declare a namespace in PHP?
Ause
Bclass
Cnamespace
Dpackage
✗ Incorrect
The namespace keyword declares a namespace in PHP.
Where must the namespace declaration appear in a PHP file?
AAt the end of the file
BBefore any other code except declare statements or comments
COnly after all functions are declared
DAnywhere in the file
✗ Incorrect
Namespace declarations must be the first statement in the file, except for declare statements or comments.
How do you declare multiple namespaces in one PHP file?
Multiple namespaces require bracketed syntax blocks for each namespace.
What symbol ends an unbracketed namespace declaration?
A;
B{}
C:
D,
✗ Incorrect
Unbracketed namespace declarations end with a semicolon.
Which of these is a valid namespace declaration in PHP?
Anamespace MyApp\Utils;
Bnamespace = MyApp.Utils;
Cnamespace: MyApp.Utils;
Dnamespace MyApp.Utils{}
✗ Incorrect
The correct syntax uses the keyword namespace followed by the name and a semicolon.
Describe how to declare a namespace in PHP and explain the difference between bracketed and unbracketed syntax.
Think about how you organize code into groups and how PHP lets you do that in one or many blocks.
You got /4 concepts.
What are the rules about where you can place a namespace declaration in a PHP file?
Consider what PHP expects at the start of a file before any code runs.
You got /3 concepts.
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
Step 1: Understand PHP namespace declaration
PHP uses the keyword namespace to declare namespaces at the top of a file.
Step 2: Identify the correct syntax
The correct syntax is namespace MyApp; with a semicolon at the end.
Final Answer:
namespace MyApp; -> Option B
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
Step 1: Recognize namespace separator in PHP
PHP uses backslashes (\) to separate nested namespaces.
Step 2: Check each option for correct separator
Only namespace MyApp\Utils; uses backslashes correctly: namespace MyApp\Utils;.
Final Answer:
namespace MyApp\Utils; -> Option D
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
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.
Step 2: Check method call and output
Calling Helper::greet() returns the string 'Hello from Helper'.
Final Answer:
Hello from Helper -> Option A
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
Step 1: Check namespace declaration syntax
The namespace declaration must end with a semicolon (;).
Step 2: Identify missing semicolon
The code misses the semicolon after namespace MyApp\Utils.
Final Answer:
Missing semicolon after namespace declaration -> Option C
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
Step 1: Understand nested namespace declaration
PHP allows declaring nested namespaces in one line using backslashes.
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.
Final Answer:
namespace Company\Project\Module;
class Feature {} -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.