New FREE PHP video tutorial of the week
So far we've looked at a number of built-in functions for PHP, but this week we're going to start writing our own! Functions are a great way to create reusable code snippets, and as we'll see in upcoming videos, they also give us some flexibility in how the code gets executed.
New PHP videos on creating custom functions
This week we start digging into creating our own custom functions. Functions give us the freedom to execute code snippets wherever we need to without copying and pasting existing code. Logically, functions help us organize functionality into reasonable pieces that will help us understand our code later, or will help other team members get a sense of what certain code is for. From this point forward, functions will form a large part of how we structure our code, so it's good to make sure you have a solid grasp of what's going on.
Now that you know how to create a function, we'll look at how to pass data to it in the form of a parameter. Parameters are just variables we can use within our functions, but being able to use them packs a lot of power when it comes to writing code.
In this video we walk through creating a classic dice roller application using a custom function. We also explore the idea of optional parameters to allow flexibility but simplify code when possible.
In this brief video, we explain how custom functions can evolve and become more powerful than using loops or copying and pasting code to get the same logic to run.
You'll probably run across references in Drupal's code before you actually have a need to use them yourself, so it's good to get a sense for what they do and how they work. References are basically variables that get passed into a function as a pointer to existing variable data. And if that doesn't make any sense, check out the video and it will soon!