0
0
PHPprogramming~10 mins

Global namespace access in PHP - Interactive Code Practice

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

Complete the code to access 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'
A$number
B$num
C$value
D$count
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name inside the function.
Not declaring the variable as global inside the function.
2fill in blank
medium

Complete the code to access the global variable using the $GLOBALS array.

PHP
<?php
$value = 5;
function printValue() {
    echo [1]['value'];
}
printValue();
?>
Drag options to blanks, or click blank then click option'
A$globals
B$GLOBALS
C$GLOBAL
D$global
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or misspelled versions of $GLOBALS.
Trying to access the variable directly without $GLOBALS.
3fill in blank
hard

Fix the error in the code to correctly access the global variable inside the function.

PHP
<?php
$count = 3;
function showCount() {
    [1];
    echo $count;
}
showCount();
?>
Drag options to blanks, or click blank then click option'
A$count
Bcount
C$this->count
Dglobal $count
Attempts:
3 left
💡 Hint
Common Mistakes
Not declaring the variable as global inside the function.
Using the variable name without the dollar sign.
Trying to access the variable as an object property.
4fill in blank
hard

Fill both blanks to create a function that modifies a global variable.

PHP
<?php
$score = 0;
function increaseScore() {
    global [1];
    [2]++;
}
increaseScore();
echo $score;
?>
Drag options to blanks, or click blank then click option'
A$score
B$count
D$value
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in the blanks.
Not declaring the variable as global before modifying it.
5fill in blank
hard

Fill all three blanks to access and modify a global variable using $GLOBALS.

PHP
<?php
$count = 1;
function addCount() {
    [1]['count'] = [2]['count'] + [3];
}
addCount();
echo $count;
?>
Drag options to blanks, or click blank then click option'
A$GLOBALS
C1
D2
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names instead of $GLOBALS.
Adding a wrong number or forgetting to add.