Did you know FaceySpacey is the creator of Redux-First Router and Universal?

The Startup Incubator.
Your one stop social media shop
Resource Categories:

Technical
Non-Technical
Refer A Friend
Popular Articles
DEVELOPMENT TOOLS 1 - VERSION CONTROL (Mercurial)
CONTRACTING 2 - TOP 10 EASY SITE CREATOR TOOLS
Subscribe
-
Technical
-
PHP
-
OOP PHP 1 - INTRO TO OBJECT ORIENTED PROGRAMMING IN PHP OOP PHP 2 - OBJECT METHODS (AKA FUNCTIONS) OOP PHP 3 - INHERITANCE IN OOP PHP 1 - Introduction To Programming PHP 2 - DYNAMIC CODE PATHS & VARIABLES PHP 3 - FUNCTIONS PHP 4 - SCOPE PHP 5 - HOW TO LEARN PROGRAMMING PHP 6 - ARRAYS PHP 7 - LOOPS PHP 8 - CONCLUSION & MORE LEARNING TECHNIQUES
- PHP 4 - SCOPE
PHP 4 - SCOPE
"Scope" is one of the first gotchas in learning programming. It has to do with when variables you've defined are available. If we re-wrote the function definition (from PHP 3 - Functions) like this:
function halveMonth($dom) { if ($dom < 15 ) {...
nothing would change. The name of the variables within the function is totally unrelated from the names outside the function, and can be the same names or different names or whatever. They are not related. THE POINT IS THIS: if for example in the function definition, we re-assigned the value of $dom, that would not change the value of $dom outside the function (unless of course the returned output was re-assigned to $dom). So that means this (let’s go back to the original code snippet and modify it a bit to use $dom inside and outside the function):
function halveMonth($dom) { $dom = 17; if ($dom < 15 ) { return ‘it’s the first half of the month’’; } else { return ‘it’s the second half of the month’; } } $dom = date('j'); $theDayOfMonth = halveMonth($dom); echo $theDayOfMonth;
will lead to $dom within the function definition having a value assigned of 17, and outside the function here $dom = date('j'); the value of dom would be 14 on the fourteenth of the month. So by assigning it 17 within the function definition, we’re not overwriting the value of $dom outside it in the “client code,” and in this case, "it's the sescond half of the month" that will be returned and assigned to the $theDayOfMonth variable. That illustrates the definition of “scope.”
If it’s not fully clear yet, it will become clear after a little more study. The important thing to implant in your head and think back to when attempting to grasp this in the future is that scope means variables at different levels of the code can have the same names and are not the same variable and not related at all--though similar names can be used to help programmers reading the code figure out what’s going on.
Related Entries
- OOP PHP 1 - INTRO TO OBJECT ORIENTED PROGRAMMING IN PHP
- OOP PHP 2 - OBJECT METHODS (AKA FUNCTIONS)
- OOP PHP 3 - INHERITANCE IN OOP
- PHP 1 - Introduction To Programming
- PHP 2 - DYNAMIC CODE PATHS & VARIABLES
- PHP 3 - FUNCTIONS
- PHP 4 - SCOPE
- PHP 5 - HOW TO LEARN PROGRAMMING
- PHP 6 - ARRAYS
- PHP 7 - LOOPS
- PHP 8 - CONCLUSION & MORE LEARNING TECHNIQUES
Comments

SMM 3 - FORMULA TO FIND INFLUENCERS