PHP Front To Back
Lesson 6 of 22 · PHP Front To Back [Part 6] – Functions · IT & Software
PHP Front To Back [Part 6] – Functions
hey guys welcome to part 6 of PHP front-to-back in this video we're going to be talking about functions okay so it's most likely going to be a really
quick video and don't worry we're almost on we're almost in the clear as far as this basic stuff variables arrays loops things like that and then we'll be looking at some cooler stuff you know
look at making post requests and working with the database things like that okay but we do have to do this this all this basic stuff for people that are brand new to see not only PHP but to
programming in general okay so I'm going to create a new file in my PHP sandbox and we're going to save this as functions dot PHP the F didn't come out okay and basically let's put in our tags
here and basically a function is pretty much just a block of code block of code that can be repeatedly called okay so we can define a function put in whatever code we want and then we can call that
throughout our script okay now there's something that I wanted to talk about earlier on Twilight for God and that is how to format or at the different ways that we can format not just functions
but also variables classes and so on so basically we have three main types that programmers use okay first of all we have camel case so chemical so camel case would look like this okay so it
starts off with a lowercase letter and then every word will have a key we'll start with a capital okay that's camel case then we also have lowercase with underscores so that would be something
like my underscore function okay so it start with the lowercase and then every word would have an underscore in between it and then they also have something called tasks okay
and that's basically camelcase what it starts with a capital as well now in PHP when it comes to variables and functions usually it's one of these two I would say that it's camelcase oh the most of
the time but you'll also see this this pascal case is usually used for classes when you use object-oriented programming and you create classes for your objects that's when you'll use this pascal case
alright but usually you'll see one of these two and we're going to use camel case that's what we've been using now when you get into frameworks different frameworks use different different
styles for instance code ignitor uses this more uses the underscore but if you get into let's say symphony or something like that they'll you use the camel case more okay
and for classes pascal case so those are the basic types okay so I just wanted to throw that in there so a function is a block of code that can be repeatedly called so to define it we use the
keyword function and then we can name it so let's just call this simple function okay so we name it and then we put our parentheses and then we have this open curly brace close curly brace and all of
our code goes in there I'm sure that I'm preaching to the choir or some of you guys I know that you know this but there are people that don't that are just learning okay so let's say for this
function all we want to do is just echo out a string we'll just echo hello world okay so that gives us a function now if I save this and we go to this the local host / PHP I was a PHP sandbox slash
functions dot PHP now we see it's not putting out hello world on the screen because all we've done to this point is define the function we haven't called it we haven't used it so if we want to call
it then we have to just use it so simple function okay just like that and then if we go and we reload we get hello world okay that runs whatever code we have in here
okay so this is create simple function and then this run simple function so that's as basic as it gets really now you can also have you can also include parameters or sometimes called arguments
and that's basically values that you can plug in to the function so let's say we want to create a function called say hello okay and then we want this to take in a parameter of a name now notice that
I use the dollar sign here because this is actually a variable so you have to include that money sign just as if you were using a variable outside of the parameters okay let me just put a
comment here so my goal is to is to for you guys to be able to have this code to look back on when you need it that's why I'm formatting it like this now when we call say hello
just like we call it simple function we can input data we can input a name so we can use that down here so let's say we want to echo things it's easier and use double quotes so we'll say echo hello
and then whatever is plugged in okay and then we'll just do a take a line break so down here let's call it will say hello and we'll plug in a name here let's say Joe and I'm just going to
comment this function out so if we reload we get hello Joe okay now we can call this as many times as we want we can say hello – Bob reload and that's going to do
hello Bob so we can call it as many times as we want in the script now let's see what happens if we run say hello but we don't put a parameter in even though it takes a parameter so let's go ahead
and run that and it gives us an error an uncaught argument count error – few arguments okay because it takes one but we're doing it to zero so what we can do here is we can actually set a default
for this if the user doesn't put in a name and we can do that by simply setting this equal to let's say world alright so we'll save that and now if we reload we get hello world because it
sees that the user didn't plug in a parameter but it sees the default of world so that's what it's going to use but then again if we go and we put something in here say Tim that's going
to overwrite world okay because world is the default this is actually what we want to use alright so pretty simple stuff and I should also mention that these are user
defined functions PHP has a whole mess of of core functions that you can use that are part of the the language okay these are all we're defining these and we've already used some for instance
when we looked at arrays we used print R and Bar dump those are those are functions that are built into PHP all right so let's comment that out now we can also we can also return values from
functions and that's usually what you do when you're when you're using when you're programming for real we're just echoing things out just to show you that it does whatever we put in here but
usually you want to get some kind of value back from the function alright so do that let me give you an example I'm just going to say function and we're going to call this add numbers and just
to show you as well that we can put more than one parameter so we'll say num1 call up num2 and then we want to first of all show you if we echo num1 plus num2 and
then down here say add numbers and can pass in anything here let's just say two and three so if we reload we get five so this is by echoing it out now if you want to return you can just use the
keyword return and then whatever you want to return now if I save this and I run it you can see it's not giving us five anymore that's because we're just returning it here
there's nothing about echoing out onto the screen which is this function is going to give us a value back so then we have to do something with that value such as echo it okay so we echo it down
here and then we get our five okay so in most cases you're going to return something from a function when we just put a comment up here okay so the last thing I want to talk about is passing
arguments by reference okay so normally if we have a variable which say my num and we'll set that to 10 so if we pass this in as an argument normally that's by value and it's not going to
manipulate this value outside of the function but we can pass it in by reference if we want to if we want it to do that so let me just give you an example because it's a little hard to
explain so let's create a function called add five and that's going to do exactly that it's gonna take in a number and then it's going to just take that number and plus equals five now if you
don't know what this is is plus equals it's the same as saying num equals num plus five it's just a shorter way of writing it so we're taking the number and adding five so let's create another
one and we're going to call it add and then same thing it's going to take a number and we're going to just say num plus equals 10 now this add 10 I'm going to pass it in
by reference by putting an ampersand loop and amber Sam in front of this okay normally it's passed in by value which is the default so what I want to do is I want to run this number through both of
these and see what we get so let's say add five and we'll pass in my num and then we'll echo my num actually let's put quotes around it and I'll just say value and then I'll do a line break and
then let's run it through add ten okay and then we'll echo that out so value sorry it's very hard to talk and type two then we'll say my num and a line break okay so it's so this value is
going through both of these functions this one is passed by reference so I'm also going to comment this out alright so let's go ahead and reload and then we get value ten value 20 so when it goes
through add five you can see that this doesn't change when we echo it out down here it's still ten but when we go through add ten it actually takes this adds ten to it and changes this to 20
because it's passed in by reference so that's the difference okay one will manipulate this number one won't now I honestly don't use this very much this this sum by reference maybe some other
programmers do I don't know I just know that the stuff that I've created with PHP I very rarely have to use that there's other ways to do things all right so but I did want to just put
it in there just to mention it all right so that's pretty much all there is to know about functions at the point at this stage so we're going to go ahead and stop here and in the next
video will most likely be talking about conditionals so if statements FL switches and also operators alright so thanks for watching and I'll see you in the next part
In this part we will look at user defined functions and some naming conventions. We are almost to the cool stuff 🙂
CODE: Code For This Video
http://www.traversymedia.com/downloads/phpsandbox/phpsandbox4.zip
COURSES: Premium Eduonix Courses
http://www.traversymedia.com/eduonix-courses
SHOW YOUR SUPPORT
https://www.patreon.com/traversymedia
ONE TIME DONATIONS:
https://www.paypal.me/traversymedia