Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m

Next lesson playing in 5 seconds

  • Overview
  • Transcript

8.3 Functions and Variables

Two important aspects of any programming language are functions and variables. In this lesson, you'll learn how to create variables to hold data and functions to perform actions.

Related Links

8.3 Functions and Variables

A couple of lessons ago, I mentioned that the server side is primarily for processing data. And in the previous lesson, we did nothing like that. We just wrote a single line of PHP code that output some HTML. And, they not very useful. So we're going to move towards the goal of taking user input and doing something with it. And in this lesson, we're going to look at two things. We're going first of all, look at creating a function and also how to create a variable. So the first thing you need to ensure is that MAMP is running, otherwise our code is not going to execute. And let's create a new file inside of our index PHP. So let's just call this message.php. And let's go ahead and start this file with the opening php tag. Now unlike the other file, we aren't going to have a closing tag because for right now, this file is going to contain nothing but pure PHP code. And it is the best practice or it is considered best practice I should say. That's for a file that has nothing but PHP code to omit the closing PHP tag. So we're going to have the opening tag. And then nothing else. Now the reason why we didn't do that in the index PHP file is because we have more than just PHP. Yes, we have PHP, but we also have some content here. So we can't just omit the ending tag, otherwise it's going to think that this is PHP code as well. So if you have a file that is nothing but PHP code, you can omit the closing tag. Okay, so let's, first of all, talk about creating a function. We starts with the keyword function, and then we have the name of our function. Now this is going to be a function that we are going to use to get a message that we will eventually display within the browser. So we're going to call it to getMessage. And we could call this function a variety of things. First of all is getMessage using what's called camel case, where you have multiple names or multiple words within the name of a function. The first word begins with the lowercase, and then all of the other words begin with an uppercase. That's why it's called camel case because we have a hump here. And if we call this getMessageToday, then we would have another hump here for today. Now used to PHP functions would look something like this. They would be all lowercase, and we would say get_message. So all of the words would be separated by an underscore. And then you will also see some functions that have nothing at all. It would just be getmessage like that. And if you go ahead and look at the PHP documentation. Let's go to php.net. If you ever want to find a function that is built into PHP, this is the place to go. Because you can go to the search box. And let's say that you want to get the time of day. Well, you can just start typing if you say gettime. And there you go. There is a function called gettimeofday. But notice that it is all lowercase. There's nothing separating the words, it is all just jammed together. However, if we look at something else. Let's look at get_browser. So here we see the underscore separating the words. And then we would find some camel casing as well, I would imagine. That's one of the things about PHP that a lot of people criticize is that it's not consistent. If you look at .net, Ruby and some other languages and platforms, the libraries that you have at your disposal are very consistent and PHP is not. You just have to know what function you're looking for, and if you don't know then you can always search for it here. Okay, so let's go back to our code. We have the function keyword, followed by name. I'm going to use camel casing here. And then we have a pair of parentheses. Now we could accept data, so that whenever we call this function, then we could pass data to it so that it could work with that data and we will eventually get there. But for right now, we're just going to have an empty parameter list. So we have nothing inside of the parentheses. And then we denote the body of the function with an opening and closing curly brace. So we've just created a function. And the let's do something. This is getting a message. So we are going to echo a message. And we'll say that this is inside getMessage. And that will be our message that we are going to get. So if we go to the browser, let's go to /message. And we aren't going to see anything at all. And that's because even though we have a function inside of our PHP code. We don't call that function. Now one other thing that I didn't really point out in the previous lesson, if you look at the source code of this page, there's nothing here. That's because that's we don't have any HTML. The only thing that we have inside of this file is PHP. And PHP never makes it to the browser. Well, there are some circumstances where your PHP code could be displayed in the browser. But for the most part, if you have PHP code that is server side code that's there for the server's purpose. Okay, so we have our function, but we haven't done anything with it. So let's go ahead and let's call that. And we simply call our function by using its name, followed by a pair of parentheses and then semicolon to end the statement. Let's save the file. We'll go back to the browser, we will refresh. And we can see our message. This is inside getMessage. So this is the basis of creating a function. You start with the function keyword followed by the name of the function. You have your parameter list contained within a pair of parentheses. We have an empty parameter list right now, and then the function body is delimited with an opening and closing curly brace. Okay, so now let's create a variable. Now some other languages have a keyword called var. And PHP has a var keyword. But if you are just creating a variable you don't use the var keyword, instead you just create the variable. And in PHP variable names begin with $ sign. This is a requirement. We can't create a variable without the $ sign. So if we said that this is a variable, it's not a variable because it doesn't begin with a dollar sign. So we'll have $ sign. We'll call this variable message. And then instead of echoing our string literal here, we're going to take that string and assign it to our message variable. So that then we can echo message. And we will essentially have the same output. The only difference here is that we have stored our message in a variable, and we are using our variable whenever we echo that message to the browser. So let's go back, let's refresh and we have the same results. So in this lesson you learned two things. How to define a function. And how to define a variable. In the next lesson, we are going to look at adding parameters to our function. We are going to allow it to accept data, so that then our function can do something with that data. So not only are we going to look at parameters, but we will also look at making decisions in PHP.

Back to the top
View on GitHub