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 3 - FUNCTIONS
PHP 3 - FUNCTIONS
So if you read the PHP 3 - Functions tutorial, you got a hint at what a function is. If you took algebra in school, you’ve already done a lot of work with functions. A function is all about input and output, which you learned about in the Introduction to Programming tutorial. So it allows you to provide some input, and depending on the input allows you to get returned a different output. In order to not introduce more things than necessary, let’s go back to the date() function.
As you may recall, we provided date() with the ‘j’ parameter like this: date(‘j’). In this example, ‘j’ is called a parameter, sometimes referred to as an argument. There is a slight difference between the terms “parameter” and “argument,” but for brevity let’s just stick with “parameter.” Basically in this example, the ‘j’ means to take the current date and return just the day of the month. If we supplied an ‘m’ like this date(‘m’), it would return the number for the day of the month. Simple as that. Different input led to different output being supplied.
The next thing to learn is that programming languages have built-in functions such as date() which as mentioned before is internally connected to the system clock of your server/computer. There are also user defined functions. To explain this we’re going to go back to the code example from the PHP 1 tutorial and turn it into a function like so:
function halveMonth($dayOfMonth) { if ($dayOfMonth < 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;
So the first thing to notice is that we got the current day of the month outside of the function. Then we passed it as a parameter to the function. The function then took this input and instead of “echoing” the result to the browser, it returned the text string (i.e. “it’s the first half of the month”). And then it assigned it to the new variable $theDayOfMonth, which subsequently was echoed to the browser.
What this allowed us to do is make a piece of re-usable code in the form of this function. Now we can call halveMonth($variable) anywhere we want and not have to re-write all the corresponding if/else code. bingo. Functions allow you to tuck code away for reuse.
The last thing I’d like to point out is how we have 3 different variables that all hold similar data:
1) $dom (the variable passed as input to the function)
2) $dayOfMonth (the variable the function deals with)
3) $theDayOfMonth (the variable the output of the function is assigned to)
The names basically all could have been the same, but I made them different so you could simply see that we were dealing with separate areas of the example. What this boils down to is a term called “scope.” Variables in the definition of the function (i.e. the code block that started with “function halveMonth($dayOfMonth)”) only pertain to that code block where the function is defined. Think about it like this: code within the function definition is very separate from code outside the function. By outside the function, i mean this code here:
$dom = date('j'); $theDayOfMonth = halveMonth($dom); echo $theDayOfMonth;
That typically would be called “client code” in terms of its relationship to halveMonth(). Just like in business a client hires a contractor to serve up some results, here the “client code” hires halveMonth() to serve up some results. So here in the world of the “client code” the variables are all in the same “scope.” That means if we re-wrote the above code like this:
$dom = date('j'); $dom = halveMonth($dom); echo $dom;
$dom will go from having an assigned value of something like “15” to having an assigned value of ‘it’s the second half of the month’. In other words, we overwrote the value of that variable. If you needed to keep the original numerical value and use the output text string at the same time, you’d use 2 different variables like I originally had. Either way, the point is that here outside the function, variables of the same name will overwrite each other when assigned new values. This is called “scope.” Continue to PHP 3 - Scope to learn more.
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
SMM 3 - FORMULA TO FIND INFLUENCERS