Functions can be a difficult topic to grasp if you're a new programmer. I'm sure you were exposed to functions in math, but do you really understand what the term "function" means?
Think about an automotive plant:
- You bring the materials to the plant.
- You feed the plant with those materials.
- The plant takes the materials and somehow produces a car (we'll be focusing on this one).
The plant itself is the function. If you were an engineer, you would know what machinery needs to go inside of the plant in order to produce the car. Since we said that the plant is the function in this case, the internals of the plant are the internals of the function. The engineer that's implementing the internals of the plant literally translates to the programmer. The programmer knows how to construct the internals of the function.
Let's create our own virtual automotive plant (function). We'll start off easy. The engineer (programmer) will create the automotive plant (function) and the only thing that the plant will do is print out "I am an auto plant." Inside of the actual plant, this could be done by a printer or even some person writing down the words. In our virtual plant (function) we can use the echo statement to print out the words.
Before we can do that, we have to understand the anatomy of the user-defined function.
function function_name() {
// function body
}
The function starts with the keyword function followed by the function name. The function name can be any valid PHP name. Function names follow the same naming convention as variables: letter or underscore, followed by a series of letters, numbers, or underscores. The parentheses come next. In this case, there are no arguments that are passed to the function (we'll cover that later). After the parentheses, the opening curly brace signifies the opening of the function body. This is where you will place your echo statement (or any other code). Function bodies can have multiple lines of code; each line of code is separated with a semi-colon (go figure). Finally, the closing curly brace is used to signify the end of the function.
Read the full article on my website
[ Ссылка ]
Code for this tutorial
[ Ссылка ]
Full Code
[ Ссылка ]
PHP Playlist
[ Ссылка ]
--
Dino Cajic
Author and Head of IT
Homepage: [ Ссылка ]
GitHub: [ Ссылка ]
Medium: [ Ссылка ]
Instagram: [ Ссылка ]
LinkedIn: [ Ссылка ]
Twitter: [ Ссылка ]
My Books
An Illustrative Introduction to Algorithms
[ Ссылка ]
Laravel Excel: Using Laravel to Import Data
[ Ссылка ]
Code Along With Me - PHP: From Basic to Advanced PHP Techniques
[ Ссылка ]
Ещё видео!