|
|
#1 (permalink) |
|
GotGames Moderator
|
Chaos' PHP Tutorial - Part 4: Functions.
Hello everyone! It's been a few weeks since my last tutorial but rest assured there are plenty more coming! Today we will be looking at functions in PHP, both custom and in-built. Functions. What is a function exactly? Well, the best way to describe a function is a segment of code which is put together in a wrapper for a purpose. We can call them to output values or do something. Functions are very useful for keeping your code neat, tidy and flexible. The Function Syntax. The syntax of functions is as follows: PHP Code:
There are the common curly brackets and inside of those go some kind of code. So how does this look in action? PHP Code:
You can then call the function like so: PHP Code:
So what's this arguments stuff I was on about before? Arguments are basically values (or variables) you can pass into a function. It's hard to explain them in any more detail, so how about I just show you. PHP Code:
Now we can call this function again. PHP Code:
My name is Chaos! We could also change it... PHP Code:
My name is Nuk3! As you can see this makes functions very versatile. However, this is not the only thing we can do with functions. Returning a Value. Rather than, say, printing out a value to a screen, we might want to return that value. Once again the best way to explain this is to demonstrate it. PHP Code:
) and also gives it two arguments; $x and $y. Then we assign the ADDITION of $x and $y to the variable of $total. We then say to return $total.PHP Code:
Code:
The variable number is equal to 0! The function has been run and now the variable of number is 30! In-Built Functions List. mail() The purpose of this function is to allow e-mails to be sent from within PHP. This is common on forums where after registration you receive an activation e-mail. PHP Code:
strtolower() This function converts a string of text into all lowercase. PHP Code:
strtoupper() Exactly the same as previous, except it converts everything to uppercase. PHP Code:
ucfirst() This function capitalizes the first character in a string and also makes every other character lowercase. PHP Code:
ucwords() The purpose of this function is to capitalize the first letter of every word in a string. PHP Code:
strlen() This function determines the amount of characters in a string. PHP Code:
include() This is a very useful function. It is used to include and evaluate other files. If you were to make a PHP file full of functions, you could use include() to import the file into another PHP file and thus make all of those functions available. PHP Code:
--Chaos. Last edited by chX; 14-05-2007 at 05:18 PM.. |
|
|
|
|
|
|
|
#2 (permalink) |
|
Pro Member
Join Date: May 2006
Location: Sydney, Australia
Posts: 6,539
Rep Power: 9
|
Might wanna mention global/static variables, and also how to set default values for the function arguments.
PHP Code:
__________________
OM NOM NOM |
|
|
|
|
|
#4 (permalink) |
|
Contributing Member
Join Date: Jun 2006
Posts: 607
Rep Power: 3
|
Hey.
A wrapper perhaps is not the best desc, however In this context it does work. Remember though that functions execute on a stack and have their own scope. Functions more aptly are pieces of code that perform a specific task and are generally quite independent of other code in the program. They can then return a value to whomever called the code and when they exit (return) all the local variables in the functions are cleaned up. When programmers talk about a wrapper we generally considered it to be (in programming speak) a piece of code that allows code/functions/classes/libraries to work together that normally would not be able to. For example, A wrapper for a C++ SSL library so it works in PHP. (in this case data marshaling and exposing an interface in php that maps to the c++ libraries interface). Some other things that should be covered: Scope - This is quite important and remember php is different to C and many other languages, where a global scope is automatically available in the local scope. Static - The static keyword in functions, for example PHP Code:
PHP Code:
![]() And perhaps you might want to put a note in about func_get_args which allows you to have a number of arguments (it can be a handy func). And lastly, yes nuk3 covered it, defaults are handy function a($need, $optional = true); And one last thing, maby people miss is the difference and uses between these functions: require include include_once require_once Might highlight that to as it is useful to any developer.
__________________
92% of teens have moved on to rap. If you are part of the 8% who still listen to real music, copy and paste this into your signature. Last edited by Khaless; 14-05-2007 at 05:58 PM.. |
|
|
|
|
|
#5 (permalink) |
|
GotGames Moderator
|
There's a lot of stuff I haven't included in the tutorials. That's mainly because it is aimed at beginners, trying to give them a little bit of insight into PHP one step at a time. That's the reason I use the analogies and descriptions or terms which people who are not adapt to PHP can relate to better.
Perhaps I can write advanced tutorials later, but at present it's just a dip in the shallow end rather than jumping into the deep end. |
|
|
|
|
|
#6 (permalink) | ||
|
Contributing Member
Join Date: Jun 2006
Posts: 607
Rep Power: 3
|
Quote:
Perhaps having a box like this: Quote:
I guess its like the lazy execution pattern.. hehe lazy learning, yep its there i know it is.. i can now learn it when i need it.
__________________
92% of teens have moved on to rap. If you are part of the 8% who still listen to real music, copy and paste this into your signature. |
||
|
|
|
|
|
#7 (permalink) |
|
Monster Member
|
Wow you guys are really smart, how much on this have you done to learn all this chaos.
__________________
Man donates blood to save his Mrs. A year later they break up. He says "give me my blood back b**ch". She throws him a tampon and says "f**k you, I'll pay you back monthly". - lividscott.
|
|
|
|
|
|
#8 (permalink) | |
|
GotGames Moderator
|
Quote:
I started by actually memorizing entire pages of code... which didn't help me at all. Sure, I could write the entire code down on notepad and produce something that works, but I lacked understanding and without understanding you can't manipulate or change the code to make something else. It was really when I took the time to understand what everything did rather than just knowing the words. |
|
|
|
|
|
|
#9 (permalink) |
|
Contributing Member
Join Date: Jun 2006
Posts: 607
Rep Power: 3
|
Seconded. Understanding is the most important thing.
To many times now i see people just cut and pasting.
__________________
92% of teens have moved on to rap. If you are part of the 8% who still listen to real music, copy and paste this into your signature. |
|
|
|
|
|
#10 (permalink) |
|
Pro Member
Join Date: Jun 2006
Age: 19
Posts: 5,593
Rep Power: 8
|
Wow, I did that too. I remember sitting in grade 4 and typing out an entire webpage with friends watching. The main focus was some Javascript buttons that took on a theme which you could change with a function call.
chem
__________________
There are no stupid questions... but there are alot of inquisitive idiots. - |
|
|
|
|
|
#12 (permalink) | |
|
Contributing Member
Join Date: Jun 2006
Posts: 607
Rep Power: 3
|
Quote:
And it becomes spaghetti code.
__________________
92% of teens have moved on to rap. If you are part of the 8% who still listen to real music, copy and paste this into your signature. |
|
|
|
|



) and also gives it two arguments; $x and $y. Then we assign the ADDITION of $x and $y to the variable of $total. We then say to return $total.

