Complete the code to print the global variable inside the function.
<?php $number = 10; function showNumber() { global [1]; echo $number; } showNumber(); ?>
The global keyword in PHP is used with the variable name without the $ sign to access the global variable inside a function.
Complete the code to return the value of the global variable inside the function.
<?php $value = 5; function getValue() { global [1]; return $value; } echo getValue(); ?>
$value with global keyword.Inside the function, use global value; without the $ to access the global variable $value.
Fix the error in the function to correctly modify the global variable.
<?php $count = 0; function increment() { [1] $count; $count++; } increment(); echo $count; ?>
local or static instead of global.To modify a global variable inside a function, you must declare it as global inside the function.
Fill both blanks to create a function that uses a static variable to count calls.
<?php
function counter() {
static [1] = 0;
[2]++;
return $count;
}
echo counter();
echo counter();
?>static keyword.The static variable $count keeps its value between function calls. We declare it with static $count = 0; and then increment it with $count++;.
Fill all three blanks to create a function that accesses a global variable and modifies it.
<?php $score = 10; function updateScore() { [1] [2]; $score [3] 5; } updateScore(); echo $score; ?>
-= instead of +=.Use global score; to access the global variable, then modify it with $score += 5; to add 5 to it.