Recall & Review
beginner
What is the purpose of a namespace in PHP?
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:<br>
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:<br>
namespace MyApp;<br>Bracketed syntax wraps code in braces to allow multiple namespaces in one file:<br>namespace MyApp { /* code */ }Click to reveal answer
Which keyword is used to declare a namespace in PHP?
✗ Incorrect
The
namespace keyword declares a namespace in PHP.Where must the namespace declaration appear in a PHP 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?
✗ Incorrect
Multiple namespaces require bracketed syntax blocks for each namespace.
What symbol ends an unbracketed namespace declaration?
✗ Incorrect
Unbracketed namespace declarations end with a semicolon.
Which of these is a valid namespace declaration in PHP?
✗ 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.