Bird
Raised Fist0
PHPprogramming~10 mins

Global namespace access in PHP - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Global namespace access
Start script
↓
Define global variable
↓
Enter function
↓
Access global variable using $GLOBALS or global keyword
↓
Modify or read global variable
↓
Exit function
↓
Use global variable outside function
↓
End script
The script defines a global variable, then inside a function accesses it using special ways, modifies or reads it, then continues outside the function.
Execution Sample
PHP
<?php
$val = 10;
function test() {
  global $val;
  $val += 5;
}
test();
echo $val;
?>
This code shows how to access and modify a global variable inside a function using the global keyword.
Execution Table
StepActionVariableValueNotes
1Define global variable$val10Global scope
2Enter function test()--Function starts
3Declare global $val inside function$val10Access global variable
4Modify $val inside function$val15Increment by 5
5Exit function test()--Function ends
6Echo $val$val15Global variable updated
7End script--Script ends
💡 Script ends after printing updated global variable value 15
Variable Tracker
VariableStartAfter function callFinal
$val101515
Key Moments - 2 Insights
Why do we need the 'global' keyword inside the function?
Without 'global', the function treats $val as a new local variable. Using 'global $val' tells PHP to use the variable from the global scope, as shown in step 3 of the execution_table.
Can we access global variables inside functions without 'global'?
Yes, by using the $GLOBALS array like $GLOBALS['val'], but this example uses 'global' keyword for simplicity (not shown in this trace).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of $val after step 4?
A15
B10
C5
DUndefined
💡 Hint
Check the 'Modify $val inside function' row in execution_table where $val changes to 15.
At which step does the function test() end?
AStep 3
BStep 4
CStep 5
DStep 6
💡 Hint
Look for the row with 'Exit function test()' in execution_table.
If we remove the 'global $val;' line, what would be the output at step 6?
A15
B10
CError
D5
💡 Hint
Without 'global', $val inside function is local and global $val remains unchanged as in variable_tracker.
Concept Snapshot
PHP global namespace access:
- Global variables exist outside functions.
- Use 'global $var;' inside functions to access global variables.
- Alternatively, use $GLOBALS['var'] array.
- Without this, functions use local variables.
- Changes inside function affect global variable if accessed properly.
Full Transcript
This example shows how PHP handles global variables inside functions. We start with a global variable $val set to 10. Inside the function test(), we use the 'global' keyword to tell PHP to use the global $val, not a new local one. We then add 5 to $val, making it 15. After the function ends, when we echo $val, it prints 15, showing the global variable was updated. Without 'global', the function would create a local $val and the global $val would stay 10. This trace helps understand how to access and modify global variables inside functions in PHP.

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