I just want to make sure we’re all on the same page. Right now I’m on a site called right PHP online. I’ll provide a link to this in the resource section of this lecture. I promised that we’ll only be using this tool for this lecture only. So the first thing I want to go over is the file constant. I’m going to echo out this constant and run the code this constant will always contain the full system path to the file it’s being used in it’s very important that you understand what the value returns when you use it. Each page we file you create will automatically define this content for you. You also don’t have to worry about conflicts between each file. Throughout this course, we’ll be working inside multiple directories. When you’re working inside multiple directories it can be rather difficult to determine where you are the final constant will help us greatly. The next concept I want to go over is arrays. I’m going to create an array named R.. And it will contain the values 1 through 5 arrays aren’t anything special and aren’t hard to understand what I want to highlight is the way I’m writing arrays. I don’t use the keyword array. Instead, I just use square brackets.
This is the shorthand way of writing arrays. I know some people like to use the array keyword but I won’t be doing so just to speed things up. If you prefer to use the array keyword then that’s perfectly fine. I do want to give a warning not all hosts support the shorthand version of writing p p arrays so use these with caution if you plan on supporting older versions of PHP. The next thing I want to go over is anonymous functions. I’m going to type one up quickly an anonymous function is exactly what it sounds like. It’s a function that does not have a name. Since it does not have a name you have no way to reference it. The reason you would use anonymous functions is to write less code. If you write less code then your code becomes more readable. I’m going to delete my example and paste in a more practical one there is quite a bit going on here so let me break down what’s going on. I’m creating a new array called array double. This will be assigned to the value returned by the function array map the array map function will take in an array and call a function that will be passed in the value to manipulate the function passed and must return either the value a modified version of the value or a completely different value. And this example I’m showing two ways of passing in a function in the first example and passing in an anonymous function in the second example I’m creating a named function and passing in the name to the array.
Matt’s function both solutions are completely valid and acceptable after both of these. All I’m doing is printing the array. Let’s run the code and see what we get. As expected we get two arrays that are identical. Both methods work. And one isn’t better than the other. You’re more than free to use either one. Throughout this course, we’ll be using a combination of both. The last concept you should be aware of is object-oriented programming also known as O P O P is a pretty complex topic and I couldn’t possibly cover it in this single lecture. Instead, I’m going to refer you to a tutorial series that is completely free in this series. A lot of things related to object-oriented programming are covered. We actually won’t be writing a lot of O P throughout this course. So you’re okay with just knowing the bare minimum. If you’re already comfortable with P then you can just skip on ahead. That’s pretty much it.
I just wanted to highlight those concepts to you. There’s one last thing I want to show you in the resource section of this lecture. I provide a link to the WordPress best practices page. WordPress provides a guideline for writing code if you plan on contributing to the world press corps. Then these guidelines are important. However, if you just plan on making your own plugins and themes then you can follow any coding style you like. I’m going to click on the P HP coding standards link. Basically, this page goes over how you should write your code from indentation to formatting blocks of
code. You are absolutely not required to follow these standards. With that being said let’s give this page a read and go over these standards together. The first thing that goes over is when to use single quotes or double quotes from the Word Press’s perspective. It generally doesn’t matter. The most important thing is that you escape your data when combining HD UML and dynamic data together.
Up next is indentation. There’s a whole debate about whether you should use tabs or spaces but WordPress recommends you use real tabs and not spaces. Something that’s also worth noting down is that WordPress prefers you to align your code for readability. And this example you can see that the equal sign is perfectly aligned no matter how long the variable name gets. Afterward you’re told You should always use braces when writing blocks of code even if you’re a block of code is only one line inside. You should still use braces. This applies to loops conditional statements functions classes and anything else you can think of that uses braces when it comes to using conditional statements. You may end up using an L if statement. You can write this one of two ways. You can write to them. Else if with space or without a space WordPress recommends no spaces. I’m going to skip regular expressions as we won’t be using the map much throughout this course. Down a little further you’ll see that you should use the long-hand version of the opening and closing HP Tex. You should never use the shorthand version because it’s not supported on most hosts. On top of that, you’ll see that you should take the time to format your code as opposed to having an unreadable mess. In this example after this, you’ll see that WordPress recommends you space everything inside functions and loops neatly omitting the spaces can really make it hard to read your code especially if you’re dealing with variables versus hard coding. Things aren’t moving right along.
You’ll then be told that you should take the time to format your Eskew l queries.
They don’t provide a good example here but you’ll see soon enough how to format your queries for readability. Right after this, you’ll be told about making queries directly to the database. Throughout most of this course, we’ll be using functions that will take care of querying the database and sanitizing the data.
However, these functions will not cover all scenarios in these cases I’ll show you how to properly query the database and make sure it’s safe for execution. Up next it’s telling us about naming conventions whenever you create variables classes or functions.
It’s recommended you use underscores if your code contains multiple words. If you’re someone who uses camel case then you may want to switch there are also rules for how you should name your files if there are multiple words. Then you should use hyphens to separate those words. I’m going to skip these next two as they’re more WordPress specific topics that will cover soon enough. Now we’re at ternary operators ternary operators or a shorthand way of writing conditional statements for setting values.
They’re really handy and can even be readable if used right. Just make sure you space everything right. And then it makes sense when you read it you’ll see plenty of examples of ternary operators throughout this course. Then we run into Yoda conditions. It’s a strange name but this simply states that when writing conditional statements with variables you should place the variables on the right side. In this example, it’s read like this. If true the force then victorious you will be. I am not really a fan of this but you can use it if it makes sense to you. Lastly, it’s telling us that you should choose readability over clever code. What this means is that you shouldn’t write your code just for the sake of writing less code while being clever with your code does mean writing less. It can also be harder to read for anyone else that’ll want to get into your code. There’s more here but we can safely ignore this. You can read them if you want.
Now I do want to say that these standards are only recommendations you won’t receive errors or warnings from writing your code differently. The point of these standards is so that there is consistency across the board at the end of the day it’s up to you. I myself follow some of these. You’ll notice that throughout this course I like to organize and format my code. You’re more than free to follow along with my coding style even though it does take some time to get used to. I believe it is worth it.
It makes my code more readable and approachable. All right that’s it. Let’s move on to developing a theme.