📖 Scenario: Imagine you are managing a small shop's sales system. You want to keep track of the total sales amount and update it when a new sale happens. But you need to understand how variables inside and outside functions behave.
🎯 Goal: You will create a variable to hold the total sales, write a function to add a sale amount, and learn how variable scope works in PHP functions.
📋 What You'll Learn
Create a variable called $totalSales and set it to 0
Create a variable called $newSale and set it to 150
Write a function called addSale that takes one parameter $amount
Inside addSale, add $amount to $totalSales using the global keyword
Call the function addSale with $newSale
Print the value of $totalSales after the function call
💡 Why This Matters
🌍 Real World
Understanding variable scope helps you manage data correctly in programs, like tracking sales or inventory in a shop system.
💼 Career
Many programming jobs require you to write functions that update shared data. Knowing variable scope prevents bugs and makes your code reliable.
Progress0 / 4 steps
1
Create initial sales variables
Create a variable called $totalSales and set it to 0. Then create a variable called $newSale and set it to 150.
PHP
Hint
Use = to assign values to variables. Remember to end each line with a semicolon.
2
Write the addSale function
Write a function called addSale that takes one parameter $amount. Inside the function, use the global keyword to access $totalSales and add $amount to it.
PHP
Hint
Use global $totalSales; inside the function to access the variable outside.
3
Call the addSale function
Call the function addSale with the variable $newSale as the argument.
PHP
Hint
Call the function by writing its name and passing $newSale inside parentheses.
4
Print the total sales
Print the value of $totalSales using echo.
PHP
Hint
Use echo $totalSales; to show the total sales amount.
Practice
(1/5)
1. What is the scope of a variable declared inside a PHP function without using global?
Variables declared inside a function are local by default, meaning they exist only within that function.
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.
Final Answer:
It is local to the function only. -> Option D
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
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.
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.
Final Answer:
Use global $count; inside the function. -> Option C
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?