
My journey as a self-taught developer began with a burning curiosity. Growing up in Uganda, where resources were scarce, I couldn't afford a formal education. Instead, I relied on handwritten notes from friends and online tutorials. As time went on, I got good at building websites for local businesses, each project proving my self-taught skills. But my dream of joining a major tech company seemed unreachable. Rejections piled up, all due to my academic background and self-doubt crept in. Then, a turning point. A friend I had shared my knowledge with landed a job at a promising startup in Kampala. He believed in me and recommended me. For five years now, I've been working remotely for that very company. They saw the potential in passionate, self-taught talent. But my heart is saddened by the millions in Uganda and the world facing the same challenges. That's why I am sharing my knowledge on this platform and other social platforms. My expertise is in PHP, JavaScript, WordPress, Technical Writing and business leadership. If you want to learn from me or collaborate, consider to follow or send me a on X.com/davidofug
The scope of a variable in PHP is the context in which it is defined and accessible. In other words, it determines where a variable can be used in the code and whether it is accessible.
In PHP, there are two types of variable scope:
① Local scope
② Global scope
Local scope means that a variable defined within a function or block of code is only accessible within that function or block of code.
A variable defined outside of a function or block of code has a global scope and can be accessed from anywhere in the code.
The global keyword in PHP also allows you to access variables from the global scope within a function or block of code. This allows you to use a variable defined in the global scope within a function or block of code without having to pass it as an argument to the function.
Want an example of how variable scope works in PHP? Here you go.
<?php
// Global scope
$city_global = 'Kampala';
function myFunction() {
// Local scope
$city_local = 'Lagos';
echo $city_local; // Outputs Lagos
echo $city_global; // Outputs Kampala (accessing a variable from the global scope)
}
myFunction();
echo $city_local; // Undefined variable error
$city_global is defined in the global scope in this example and is accessible from within the myFunction function. $city_local is defined in the myFunction function and is only accessible within it. If you try to access $city_local outside of the function, an undefined variable error will occur.
global scope
------------
| |
| function |
| |
------------
local scope
In this example, the global scope represents the main program or script that is running. The function represents a function defined within the program. The local scope represents variables that are defined within the function and are only accessible within that function.
Variables defined in the global scope are accessible to all functions and can be accessed and modified by them. Variables defined in the local scope are only accessible within the function in which they are defined, and they cannot be accessed or modified by functions outside of that function.
I hope this helps! Please let me know if you have any further questions in the comments below.



