0
0
PHPprogramming~5 mins

Namespace declaration syntax in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Ause
Bclass
Cnamespace
Dpackage
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
How do you declare multiple namespaces in one PHP file?
AUsing the use keyword multiple times
BUsing multiple unbracketed namespace declarations
CIt is not possible
DUsing bracketed syntax for each namespace block
What symbol ends an unbracketed namespace declaration?
A;
B{}
C:
D,
Which of these is a valid namespace declaration in PHP?
Anamespace MyApp\Utils;
Bnamespace = MyApp.Utils;
Cnamespace: MyApp.Utils;
Dnamespace MyApp.Utils{}
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.