How To Create Functions In PHP - Functions Tutorial - Full PHP 8 Tutorial

preview_player
Показать описание
In this PHP tutorial, you will learn what functions are & how to create them. In addition to creating functions, you will learn about type hinting, return types, inner functions, union types & so on. In the next couple of videos, you will learn a lot more about functions & how you can use them properly so stay tuned.

SOME OF THE WAYS YOU CAN SUPPORT THE CHANNEL
👍 Smash the like button
🤝 Subscribe to the channel & turn the notifications on
💬 Post comments, any feedback is greatly appreciated
THANK YOU!

LESSON 1.21

CHAPTERS
00:00 - Functions
00:36 - Creating functions
01:10 - Return values from functions
01:52 - Where to define functions
02:14 - Declaring functions conditionally
02:49 - Inner functions
03:47 - Functions with the same name
04:21 - Return types & strict types
06:13 - Nullable types
06:43 - Union types (type hinting multiple types)
07:05 - Mixed type
Рекомендации по теме
Комментарии
Автор

❤️ at this moment I am liking and commenting before watching the video just to make sure I don't forget before moving to the next

omaryahia
Автор

Excellent video. These videos are so detailed that all my questions are answered with so many code samples. Thank you so much for taking the time to make these.

longkesh
Автор

I love this series of php tutorial. Thank you so much, and it REALLY REALLY helps me a lot!!!

sunflower-ghy
Автор

If someone needs a transcript for this video here it is:
Functions in PHP are similar to functions in other programming languages. It basically is a block of code which can take an input, do some magic and then return a value. There are a lot of built-in functions available and ready to use in PHP but you could also create your own custom functions. You've already seen me create functions and use some of the built-in functions in previous lessons. But in the next few videos, we'll go into more detail on different types of functions, typed parameters, return types, variable scopes, anonymous functions, variable functions, arrow functions and so on. You can create a function by using the function keyword, followed by the name of the function which should start either with an underscore or a letter, followed by the list of parameters that it accepts within the parentheses separated by commas. This is optional so you don't have to define any parameters. And finally, you put your block of code within the curly braces. Let's echo out hello world and refresh the page and we don't see anything on the page. That's because after you declare a function, you need to actually invoke it for you to execute. And by invoking, I mean you need to call that function. We can call it from here, refresh the page and now we see hello world. Instead of printing something directly from the functions, you could actually return something from the function and then do something with that value. So instead of echoing something out, we could do return and now nothing will be printed on the screen, but this actually returns a value. So we could assign it to a variable like this and then echo out x or we can just directly echo it out like this and it will still work. The return statement is optional. So if you don't use it or you don't specify the value to return, then the null value will be returned by default. So for example, if we remove this hello world from here and we var dump full, we refresh, we get null. If we remove the return statement and we refresh, it's still null. So let's change this back to returning something like hello world and refresh. And as you notice, I defined a function on the top before I used it. Now this is not a requirement. You could define the function after like this and it will still work. If you refresh, everything still works. That means that you could define functions at the top or in the middle or at the bottom and they will always be loaded by the PHP first. So they could be used as long as the function has been loaded to the script that calls it. There are a couple of exceptions though. The exception is that if you're defining a function conditionally, then the condition has to happen and pass before you call that function. For example, we could put this function declaration within a condition if false and then do it like that. And of course, this would be some kind of expression, right? Now, if we try to call full, we're going to get an undefined function error. If we set this to true and refresh the page, we'll still get that undefined function error because this has to run first before we try to run the function. So if we remove this and put it down, now everything will work. So just something to keep in mind if you're defining functions conditionally. Another exception is when declaring functions within the functions. Because in PHP, you're able to have functions within the functions. And because functions in PHP have global scope, they can be called from outside of the function as long as the first function was called first. So let me show you what I mean. Let's remove this conditional from here and let's define another function here called bar. And let's echo out bar and let's echo out full here. And we're just going to call full. If we refresh the page, we get full. This function here doesn't get executed because we haven't invoked it yet. If we try to call bar here, now it will work. We'll get full and then bar. However, if we don't call the full first and we try to call bar, we're going to get that undefined function error. That's because the full hasn't running it and therefore this function hasn't been declared. So in order for this to work, you need to call full function first. I would not recommend defining your functions conditionally or defining functions within the functions. It just makes code harder to read. One thing to note is that you cannot have two functions with the same name. So for example, we have full bar and within bar, let's say you had another function called full and we try to call full. Now this will not give you any errors because this is executing this function and it's printing full. If we execute the function bar here, we refresh the page. We're going to get that cannot re-declare the function full. Because now we have a function called full, we call that function which declares function bar and then we call that function bar which declares function called full but we already have a function named full and therefore it's throwing an error. Alright, so next let's talk about the return types. You're able to type in the return value. So what I mean by that is that right now we're returning one, right? But we're not actually telling PHP what the type of this one is. PHP will try to dynamically figure out what the type is and if we refresh the page, we'll get the integer. But we could actually type hint this and the way you can do that is by using colon right after closing parenthesis and then add the expected data type. So in this case, let's do int. So now we're telling PHP that the expected data type of the return value is integer. If we refresh the page, everything still works the same. Now what if we change this to a string one and we refresh the page? It still works, right? We're not getting any errors. And that's because we're not using strict types. Remember a couple lessons before we enabled strict types? Well, right now we haven't enabled the strict types. So basically PHP will still try to figure out the data type on its own when possible. If it cannot figure out the data type, then it will throw an error. So if we change this to something like an array, now this is going to throw an error because array cannot be converted to an integer. Again, if you want to enforce the strict types, it could declare strict types one. And now if we refresh the page, we're going to get an error that the expected type is integer, but the string was returned instead. So we'll need to change it to an integer. So how would you type in something when you're not returning anything? So if you did something like this, what would you type in here? Well, PHP allows you to type hint void and that basically means that nothing is expected to be returned. If we refresh the page, we're going to get null. And that's because, as I mentioned before, when you're not returning anything, the default return will be null. When you're type hinting void and you remove the return statement, it will still work. But it will not work if you try to return null explicitly. If we refresh the page, we're going to get an error. Even though by default it returns null, when you try to return null explicitly, it is no longer void because you're actually trying to return some kind of value. And therefore it will get an error. If you wanted to type in something like integer, but then you expect to have nullable values, meaning that in some cases you might want to return null, but other cases you want to return an integer. Luckily in PHP, there is something called nullable types where you can prefix your type with a question mark. And that indicates that along with the actual type, the null value is also acceptable. This will work because we're returning null, but it will also work when you're returning an integer. If you don't use the question mark and you try to return null, you're going to get an error. Since PHP 8, you could actually type hint multiple types separated by the pipe character. So for example, what if we wanted to return integer or floating number? We could do int, pipe, float, and now we could return either an integer and it will work, or we could return a floating number and it will still work. You could say that you might also expect an array and return an array instead and refresh and it will still work. Now, if you're going to be expecting a lot of different types, then you might not need to type in this many data types. Instead, you could use something called mixed, which is also available since PHP 8. And that basically accepts multiple data types. So you could return an array, it will still work, it can return an integer, it will work, it can return a floating number, and it will still work. I personally don't like using mixed because I like to be explicit on what types I'm expecting. But there are some use cases for mixed, which is why it was introduced in PHP 8. One of those use cases could be that you're expecting a type that can't be type hinted in PHP. Also note that because mixed already includes the null, you cannot mark mixed as a nullable type, meaning that you cannot use question mark with it. Similar to type hinting the return values, you could also type hint to parameters. And we're going to talk about defining parameters, accepting arguments, and more cool stuff in the next video. If you like my tutorials, please give this video a thumbs up, share, and subscribe, and I will see you on the next one.

benderbg
Автор

Much gratitude for this series. Thank you man! Wishing you much abundance in all dimensions of life!

only_visiting
Автор

No words! This is indeed the right way to learn PHP!!!

ינוןאלבז-כז
Автор

Your course is much more complicated than others

MaziarHeidari-ie
Автор

wow, learned so much here, i had no clue

niksatan
Автор

thank you so much bro >> allah bless you 💚

mahmoudtaha
Автор

It's hard to overestimate the importance of functions!

Vitalii-mr
Автор

we have added functions to your functions so that you can call functions while calling functions.

knyazgame
Автор

Man, I don't understand why this course is here and not on Udemy.
You could do the basic part on YouTube, and do the rest for less on Udemy.

This course would have been the best there.

AO-ctjs
Автор

Thank you!
Where can i read more about "Return types & strict types"?
the ": int"

ahmedmahdy
Автор

DO YOU CAN PUT PORTUGUESES SUBTITLES IN YOUR VIDEOS? PLS.... i'm from brasil, and you are the best youtuber progammer that i know. but will be better with your subtitles, because have some videos that the youtube default subtitle dont work...

posinfo
Автор

Good work just we need practice practice practice hahaha I hope to do exercises parallel with the lesson that's will be so great

mohamed-aminebenhima
Автор

Thanks for this tutorial. I am looking to create a function for a select query, PDO connection and prepared statements. Do you have any tutorials on this or would you consider making one? Thanks

DavidAshby
Автор

Why some videos don't have subtitles?

shileili
Автор

what the difference between using mixed hint and without using any hints

skywalker
Автор

function foo(): int {return 1;} - in this function :int - this part, or :mixed - in PHPstorm show me warning that int or mixed available in PHP 7 only, but sandbox dont show any warning

ejafarow
Автор

DO YOU CAN PUT PORTUGUESES SUBTITLES IN YOUR VIDEOS? PLS.... i'm from brasil, and you are the best youtuber progammer that i know. but have some videos that the youtube default subtitle dont work...

posinfo
join shbcf.ru