Bird
Raised Fist0
PHPprogramming~20 mins

Global namespace access in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Global Namespace Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of accessing global namespace function
What is the output of this PHP code?
PHP
<?php
namespace MyApp;
function strlen($str) {
    return "MyApp strlen: " . strlen($str);
}

echo strlen("test");
echo "\n";
echo \strlen("test");
echo "\n";
echo \MyApp\strlen("test");
?>
A
MyApp strlen: 4
4
4
B
MyApp strlen: 4
4
MyApp strlen: 4
C
4
4
MyApp strlen: 4
D
4
MyApp strlen: 4
4
Attempts:
2 left
💡 Hint
Remember that \strlen calls the global function, while strlen inside namespace calls the local one.
❓ Predict Output
intermediate
2:00remaining
Accessing global constant inside namespace
What will this PHP code output?
PHP
<?php
namespace Test;
const PI = 3.14;

echo PI . "\n";
echo \PI . "\n";
?>
A
3.14
Fatal error: Uncaught Error: Undefined constant PI
B
3.14
PHP Notice: Undefined constant PI
C
3.14
1
D
3.14
3.14
Attempts:
2 left
💡 Hint
Global constants are not automatically imported into namespaces.
🔧 Debug
advanced
2:00remaining
Why does this global function call fail?
This code throws an error. What is the cause?
PHP
<?php
namespace App;
function test() {
    echo "App test";
}

function run() {
    test();
    \test();
}

run();
?>
ACalling test() fails because it requires parentheses.
BCalling test() fails because it is ambiguous inside the namespace.
CCalling \test() fails because global functions cannot be called inside namespaces.
DCalling \test() fails because there is no global function named test.
Attempts:
2 left
💡 Hint
Check if the global namespace has a function named test.
📝 Syntax
advanced
2:00remaining
Which option correctly accesses a global class inside a namespace?
Given a global class Logger, which code correctly creates an instance inside namespace App?
PHP
<?php
namespace App;
class Logger {
    public function log() {
        echo "App Logger";
    }
}

// Global Logger class
class \Logger {
    public function log() {
        echo "Global Logger";
    }
}

// Which line correctly creates a global Logger instance?
// $logger = ???;
?>
A$logger = new \Logger();
B$logger = new Logger();
C$logger = new \App\Logger();
D$logger = new \global\Logger();
Attempts:
2 left
💡 Hint
Use a leading backslash to access global classes from inside a namespace.
🚀 Application
expert
3:00remaining
How to call a global function named 'process' inside a namespace with a local function of the same name?
You have a global function process() and inside namespace Data you also have a function process(). How do you call the global process() from inside Data namespace?
PHP
<?php
function process() {
    return "global process";
}

namespace Data {
    function process() {
        return "Data process";
    }

    function run() {
        // Call global process here
        return ???;
    }
}

echo \Data\run();
?>
Aprocess()
Bglobal\process()
C\process()
DData\process()
Attempts:
2 left
💡 Hint
Use a leading backslash to call global functions inside namespaces.

Practice

(1/5)
1. What does the leading backslash \ do when used before a function name inside a PHP namespace?
easy
A. It creates a new function inside the current namespace.
B. It comments out the function call.
C. It calls a function from the current namespace only.
D. It accesses the function from the global namespace.

Solution

  1. Step 1: Understand namespaces in PHP

    Namespaces group code to avoid name conflicts but can hide global functions.
  2. Step 2: Role of leading backslash

    The leading backslash tells PHP to look for the function in the global namespace, ignoring the current namespace.
  3. Final Answer:

    It accesses the function from the global namespace. -> Option D
  4. Quick Check:

    Leading backslash = global access [OK]
Hint: Leading \ means use global function, not namespaced one [OK]
Common Mistakes:
  • Thinking \ creates a new function
  • Assuming \ calls current namespace function
  • Confusing \ with escape character
2. Which of the following is the correct syntax to call the global strlen function inside a namespace?
easy
A. \strlen('test');
B. namespace\strlen('test');
C. strlen('test');
D. global strlen('test');

Solution

  1. Step 1: Identify global function call syntax

    To call a global function inside a namespace, prefix it with a backslash \.
  2. Step 2: Check each option

    \strlen('test'); uses \strlen('test'); which correctly calls the global strlen function.
  3. Final Answer:

    \strlen('test'); -> Option A
  4. Quick Check:

    Backslash before function = global call [OK]
Hint: Use \ before function name to call global version [OK]
Common Mistakes:
  • Omitting the leading backslash inside namespaces
  • Using 'global' keyword incorrectly
  • Trying to prefix with 'namespace' keyword
3. What will be the output of this PHP code?
namespace MyApp;
function strlen($str) {
    return 42;
}

echo strlen('hello');
echo "-";
echo \strlen('hello');
medium
A. 42-5
B. 5-5
C. 42-42
D. 5-42

Solution

  1. Step 1: Understand function calls inside namespace

    Calling strlen('hello') calls MyApp\strlen which returns 42.
  2. Step 2: Understand global function call

    Calling \strlen('hello') calls global strlen which returns length 5.
  3. Final Answer:

    42-5 -> Option A
  4. Quick Check:

    Namespaced strlen = 42, global strlen = 5 [OK]
Hint: No \ calls local, \ calls global function [OK]
Common Mistakes:
  • Assuming both calls return same value
  • Mixing up which strlen is called
  • Ignoring namespace effect on function calls
4. Identify the error in this PHP code snippet inside a namespace:
namespace Test;
function array_sum($arr) {
    return 0;
}

$result = array_sum([1, 2, 3]);
$result2 = \array_sum([1, 2, 3]);
echo $result . ',' . $result2;
medium
A. Runtime error because \array_sum is undefined.
B. Syntax error due to missing semicolon.
C. No error, code runs and outputs '0,6'.
D. Namespace keyword used incorrectly.

Solution

  1. Step 1: Check function definitions and calls

    array_sum is redefined in namespace Test to always return 0.
  2. Step 2: Understand calls

    array_sum([1,2,3]) calls namespaced function returning 0; \array_sum calls global function returning 6.
  3. Final Answer:

    No error, code runs and outputs '0,6'. -> Option C
  4. Quick Check:

    Namespaced and global functions both called correctly [OK]
Hint: Leading \ calls global function, no syntax error [OK]
Common Mistakes:
  • Thinking \array_sum is undefined
  • Expecting syntax errors from backslash
  • Confusing namespace keyword usage
5. You have a namespaced class MyApp\Logger and a global class Logger. How do you instantiate the global Logger inside the MyApp namespace?
hard
A. $log = new Logger();
B. $log = new \Logger();
C. $log = new MyApp\Logger();
D. $log = new global\Logger();

Solution

  1. Step 1: Understand class instantiation in namespaces

    Without backslash, new Logger() looks for MyApp\Logger inside the namespace.
  2. Step 2: Use leading backslash to access global class

    new \Logger() creates an instance of the global Logger class.
  3. Final Answer:

    $log = new \Logger(); -> Option B
  4. Quick Check:

    Leading \ before class = global class instantiation [OK]
Hint: Use \ before class name to instantiate global class [OK]
Common Mistakes:
  • Omitting \ and getting namespaced class
  • Using 'global\' which is invalid
  • Confusing namespace prefix with global access
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