📖 Scenario: Imagine you are building a simple PHP application that tracks user visits. You want to understand how using global variables can cause problems when multiple parts of your program change the same data unexpectedly.
🎯 Goal: You will create a PHP script that uses a global variable to count visits, then see how changing it in different functions can lead to confusing results. This will help you learn why global state is dangerous and how to avoid it.
📋 What You'll Learn
Create a global variable called $visitCount initialized to 0
Write a function incrementVisit() that increases $visitCount by 1 using the global keyword
Write a function resetVisit() that sets $visitCount to 0 using the global keyword
Call incrementVisit() twice, then resetVisit(), then incrementVisit() once
Print the final value of $visitCount
💡 Why This Matters
🌍 Real World
Many PHP applications use global variables to share data, but this can cause bugs when different parts of the code change the same data unexpectedly.
💼 Career
Understanding why global state is dangerous helps developers write cleaner, more reliable PHP code, which is important for maintaining and scaling web applications.
Progress0 / 4 steps
1
Create the global variable $visitCount
Create a global variable called $visitCount and set it to 0.
PHP
Hint
Use $visitCount = 0; outside any function to create a global variable.
2
Write the functions incrementVisit() and resetVisit()
Write a function called incrementVisit() that uses the global keyword to access $visitCount and adds 1 to it. Also write a function called resetVisit() that uses the global keyword to set $visitCount back to 0.
PHP
Hint
Inside each function, use global $visitCount; to access the global variable.
3
Call the functions in order
Call incrementVisit() twice, then call resetVisit(), then call incrementVisit() once.
PHP
Hint
Call the functions exactly in this order: incrementVisit(), incrementVisit(), resetVisit(), incrementVisit().
4
Print the final value of $visitCount
Print the value of the global variable $visitCount using echo.
PHP
Hint
Use echo $visitCount; to show the final count.
Practice
(1/5)
1. Why is using global variables considered dangerous in PHP?
easy
A. Because they can be changed anywhere, causing unexpected bugs
B. Because they make the code run faster
C. Because they prevent the use of functions
D. Because they automatically encrypt data
Solution
Step 1: Understand what global variables do
Global variables can be accessed and modified from any part of the program.
Step 2: Identify the risk of global variables
Since they can be changed anywhere, it is easy to accidentally overwrite or misuse them, leading to bugs that are hard to find.
Final Answer:
Because they can be changed anywhere, causing unexpected bugs -> Option A
Quick Check:
Global variables cause unexpected bugs [OK]
Hint: Global variables can be changed anywhere, causing bugs [OK]
Common Mistakes:
Thinking global variables improve performance
Believing global variables prevent function use
Assuming global variables encrypt data automatically
2. Which of the following is the correct way to access a global variable inside a PHP function?
easy
A. Declare the variable again inside the function
B. Use the global keyword inside the function
C. Use static keyword inside the function
D. Use const keyword inside the function
Solution
Step 1: Recall PHP syntax for accessing global variables
In PHP, to access a global variable inside a function, you must declare it with the global keyword.
Step 2: Understand why other options are incorrect
Declaring the variable again inside the function creates a new local variable. static keeps value between calls but does not access global. const defines constants, not variables.
Final Answer:
Use the global keyword inside the function -> Option B
Quick Check:
Access global variable in function = global keyword [OK]
Hint: Use 'global' keyword inside function to access globals [OK]
Common Mistakes:
Redeclaring variable inside function thinking it accesses global
Using static or const keywords incorrectly
Forgetting to declare global keyword inside function
3. What will be the output of this PHP code?
$count = 5; function increment() { global $count; $count++; } increment(); echo $count;
medium
A. 5
B. Nothing is printed
C. Error: Undefined variable
D. 6
Solution
Step 1: Understand the global keyword usage
The function declares global $count; which means it uses the global variable $count defined outside.
Step 2: Trace the code execution
The global $count starts at 5, the function increments it by 1, so $count becomes 6. Then echo $count; prints 6.
Final Answer:
6 -> Option D
Quick Check:
Global increment changes $count to 6 [OK]
Hint: Global keyword lets function modify external variable [OK]
Common Mistakes:
Thinking $count inside function is local without global keyword
Expecting output 5 because of misunderstanding scope
Assuming code causes error due to variable scope
4. Identify the problem in this PHP code related to global state: