22: How to Create Your Own Function in PHP | PHP Tutorial | Learn PHP Programming | PHP Lesson

How to Create Your Own Function in PHP | PHP Tutorial | Learn PHP Programming | PHP Lesson. A function is used in PHP to run a script that has a specific purpose in your code. Today we will learn about functions and how to use them in our websites.


First of all, thank you for all the support you have given me!

I am really glad to have such an awesome community on my channel. It motivates me to continue creating and uploading content! So thank you!

I am now using Patreon to share improved and updated lesson material, and for a small fee you can access all the material. I have worked hard, and done my best to help you understand what I teach.

I hope you will find it helpful 🙂

Material for this lesson: https://www.patreon.com/posts/php-22-download-15629531


Vijay deshmane says:

best tutorial

Ramon Fernandez says:

*Expand this comment if you’re a beginner who is confused.

*Note for non-beginner programmers: from within the local scope of a function, a global variable can only be accessed if you first write the keyword ‘global’ before the variable name. There’s also a PHP defined array named $GLOBALS that uses the name of the globally defined variable as the accessor(e.g., $GLOBALS[‘varName’])

I love all of your videos so far. I think that this video is treading dangerous territory. I’m sure you go over this later on(confirmed that he does), but I believe this video should have directly addressed what variable scope is. The explanation of functions was uncharacteristically unorganized from what I’ve seen of your tutorials. I believe you should have explained functions that don’t take arguments. Then explained functions that do take arguments. Then explained that functions can return values as well.

I know when teaching about programming, sometimes it’s best to just say “we’ll explain why later”. In this case, I think the order in which things are discussed could easily be confusing.

If you are confused I recommend looking at this page: https://www.w3schools.com/php/php_variables.asp
Specifically the section about scope that talks about local and global scope and how to access a global variable from within a function.

Also look at this page: https://www.tutorialspoint.com/php/php_functions.htm
Specifically the sections: Creating PHP Function, PHP Functions with Parameters, Passing Arguments by Reference, and PHP Functions returning value.

It may seem like it’s a lot of material, but It’s all straightforward and are consistent properties of most programming languages, which is why even though I’m learning php like you, I understand those concepts. I don’t think it will take much time at all to absorb what you need to know.

Nigel Pallatt says:

echo “75% of $x is $newnr”; Use dubel quotes then you dont need the , or .

KAustin says:

you can also do this:
< ?php function newCalc($x) { $newnr = $x * 0.75; echo "Here is 75% of what you wrote: ".$newnr; } $x = 100; $y = 1000; if ($y == 1000) { newCalc($x); } ?>

Andy Chen says:

Hi, Daniel. Your PHP tutorial is one of the best I have watched. Thank you. Just curious, where you from?

Eos April says:

Do you have Class || OOP? thx 😉

ChaoticTrack says:

Echoing rathering than returning a value is known as a side-effect of a function which generally goes against your original point.

Funky Attitude says:

I just wanna thank you for these awesome tutorials and also thank you for replying my question. I couln’t be enjoying this more than I am right now 🙂

Md. Morshed Alam says:

Hi, thank you for the tutorial.. but have a question..

you created a function with variable $x.. after that you changed the variable to $a.. but you did not change the insider of that function.. but it worked…! how? need a little bit more please.. 🙂

I’m new in php, you can tell.

R Williams says:

good stuff…what I don’t really get is that lessons seem to show how code reacts to values put in by the programmer….  how do values (that always change) by random users on a website for example get processed?

Eos April says:

When you finish the beginer tutorials try sell the next tutorials advanced, i will definitely buy, your tutorials are the best.

MightKeeper says:

You didn’t mention the use of return inside a function, and the possibility of inputing different parameters separated by a comma.
Thats something pretty basic.

 Write a comment


Do you like our videos?
Do you want to see more like that?

Please click below to support us on Facebook!