Bird
Raised Fist0
PHPprogramming~10 mins

Variable scope in functions in PHP - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print the global variable inside the function.

PHP
<?php
$number = 10;
function showNumber() {
    global [1];
    echo $number;
}
showNumber();
?>
Drag options to blanks, or click blank then click option'
Anumber
B$number
C$this
Dvar
Attempts:
3 left
💡 Hint
Common Mistakes
Including the $ sign when declaring a global variable inside a function.
Using a different variable name inside the function.
2fill in blank
medium

Complete the code to return the value of the global variable inside the function.

PHP
<?php
$value = 5;
function getValue() {
    global [1];
    return $value;
}
echo getValue();
?>
Drag options to blanks, or click blank then click option'
Avalue
Bthis
C$value
Dvar
Attempts:
3 left
💡 Hint
Common Mistakes
Using $value with global keyword.
Trying to access the variable without declaring it global.
3fill in blank
hard

Fix the error in the function to correctly modify the global variable.

PHP
<?php
$count = 0;
function increment() {
    [1] $count;
    $count++;
}
increment();
echo $count;
?>
Drag options to blanks, or click blank then click option'
Alocal
Bglobal
Cstatic
Dpublic
Attempts:
3 left
💡 Hint
Common Mistakes
Using local or static instead of global.
Not declaring the variable as global and trying to modify it.
4fill in blank
hard

Fill both blanks to create a function that uses a static variable to count calls.

PHP
<?php
function counter() {
    static [1] = 0;
    [2]++;
    return $count;
}
echo counter();
echo counter();
?>
Drag options to blanks, or click blank then click option'
A$count
B$counter
D$calls
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for declaration and increment.
Not using the static keyword.
5fill in blank
hard

Fill all three blanks to create a function that accesses a global variable and modifies it.

PHP
<?php
$score = 10;
function updateScore() {
    [1] [2];
    $score [3] 5;
}
updateScore();
echo $score;
?>
Drag options to blanks, or click blank then click option'
Aglobal
Bscore
C+=
D-=
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to declare the variable as global.
Using the wrong operator like -= instead of +=.

Practice

(1/5)
1. What is the scope of a variable declared inside a PHP function without using global?
easy
A. It is accessible only outside the function.
B. It is global and accessible everywhere.
C. It is static and retains value between calls.
D. It is local to the function only.

Solution

  1. Step 1: Understand variable declaration inside functions

    Variables declared inside a function are local by default, meaning they exist only within that function.
  2. Step 2: Recall the role of the global keyword

    Without using global, the variable does not refer to any global variable with the same name.
  3. Final Answer:

    It is local to the function only. -> Option D
  4. Quick Check:

    Local scope = inside function only [OK]
Hint: Variables inside functions are local unless declared global [OK]
Common Mistakes:
  • Assuming variables inside functions are global by default
  • Confusing local variables with static variables
  • Thinking variables inside functions affect global variables automatically
2. Which of the following is the correct way to access a global variable $count inside a PHP function?
easy
A. Use var $count; inside the function.
B. Declare $count again inside the function.
C. Use global $count; inside the function.
D. Use static $count; inside the function.

Solution

  1. Step 1: Recall how to access global variables inside functions

    In PHP, to access a global variable inside a function, you must declare it with the global keyword inside that function.
  2. Step 2: Check other options for correctness

    Declaring the variable again inside the function creates a new local variable. static keeps value between calls but does not access global. var is not used inside functions.
  3. Final Answer:

    Use global $count; inside the function. -> Option C
  4. Quick Check:

    Access global with global keyword [OK]
Hint: Use 'global' keyword to access global variables inside functions [OK]
Common Mistakes:
  • Re-declaring global variables inside functions without 'global'
  • Using 'static' instead of 'global' to access globals
  • Using 'var' keyword inside functions
3. What will be the output of the following PHP code?
$x = 5;
function test() {
    $x = 10;
    echo $x;
}
test();
echo $x;
medium
A. 10 5
B. 5 10
C. 10 10
D. 5 5

Solution

  1. Step 1: Analyze variable scope inside the function

    Inside test(), $x is assigned 10 locally, so echo $x; prints 10.
  2. Step 2: Analyze variable outside the function

    Outside the function, $x remains 5 because the local $x inside the function does not affect the global $x.
  3. Final Answer:

    10 5 -> Option A
  4. Quick Check:

    Local $x = 10, global $x = 5 [OK]
Hint: Local variables inside functions do not change globals [OK]
Common Mistakes:
  • Assuming function changes global variable without 'global' keyword
  • Confusing output order
  • Thinking variables inside functions share the same memory as globals
4. Identify the error in the following PHP code and select the correct fix:
$count = 0;
function increment() {
    $count++;
}
increment();
echo $count;
medium
A. Declare $count as static inside the function.
B. Add global $count; inside the function.
C. Pass $count as a parameter to the function.
D. No error; code works fine.

Solution

  1. Step 1: Understand variable scope in the function

    The function tries to increment $count, but without global, it treats $count as undefined local variable.
  2. Step 2: Fix the scope issue

    Adding global $count; inside the function tells PHP to use the global $count variable, allowing increment to work.
  3. Final Answer:

    Add global $count; inside the function. -> Option B
  4. Quick Check:

    Use global keyword to modify global variables inside functions [OK]
Hint: Use 'global' to modify global variables inside functions [OK]
Common Mistakes:
  • Forgetting to declare global variables inside functions
  • Assuming variables are automatically global
  • Using static instead of global for global variables
5. Consider the following PHP code:
$value = 3;
function multiply() {
    global $value;
    $value *= 2;
}
multiply();
function reset() {
    $value = 0;
}
reset();
echo $value;
What will be the output and why?
hard
A. 6 because multiply changes global, reset changes local only.
B. 0 because reset changes global variable.
C. 3 because multiply and reset do not affect global variable.
D. Error because reset tries to access undefined global variable.

Solution

  1. Step 1: Analyze multiply() function

    It uses global $value; and doubles the global $value from 3 to 6.
  2. Step 2: Analyze reset() function

    It declares $value without global, so it creates a local variable $value set to 0, leaving global $value unchanged.
  3. Step 3: Final output

    After calling both functions, global $value remains 6, so echo $value; prints 6.
  4. Final Answer:

    6 because multiply changes global, reset changes local only. -> Option A
  5. Quick Check:

    global keyword affects global variable, local does not [OK]
Hint: Only functions with 'global' keyword affect global variables [OK]
Common Mistakes:
  • Assuming reset() changes global variable without 'global'
  • Thinking variables inside functions always affect globals
  • Confusing local and global variable effects
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