PDA

View Full Version : PHP Functions


Rikeku
09-17-2006, 04:45 PM
**Welcome**

I'm learning PHP and decided i would do a little tutorial on Functions in PHP. A lot of you probably already know what functions are, but some of you might not.
here is some example code on finding the length of a string.


<?php
echo strlen('hello');
?>


the result when you loaded the php script into your web browser would be

--
5
--

there is an example of a built in function in PHP. It takes one ARGUMENT, “hello”, and carries out the rest of the little program. Which in this case yields the length of the string “hello” which is five.

There are so many built in functions that are useful. But at some point or another your going to want to be able to make your own little functions to carry out specific processes. Here is an example of the hello World program.


<?php
echo “Hello World!”;
?>


which would yield

--
Hello World!
--

If i was coding something where i would be printing out “Hello World!” a bunch of times, I would take it upon myself to code a function to do it for me. Like So.


<?php
function hello_world() {
echo “Hello World!
”;
}
//now that i have the function written i could print this as many times as i wanted just by typing:
hello_world();
hello_world();
hello_world();
?>


which would yield:

--
Hello World!
Hello World!
Hello World!
--

there was about as simple as functions get. I didn't even have to pass any arguments through the function.
Here is an example of a simple function that you might create with arguments passing though.


<?php
function print_this($arg1) {
echo $arg1;
}
//now i would have to pass an argument through to get it to print anything.
print_this("Hello World!");
?>


which would yield...you guessed it:

--
Hello World!
--

you can also call functions from inside other functions

That concludes the basics on making your own funcitons in PHP.
Functions are a vital part of programming. They make certain tasks easier to perform multiple times and clean up code.

SaIRoS
09-17-2006, 04:52 PM
Hey, nice tut Rikeku ;)

:thanks:

geepee
09-17-2006, 07:22 PM
ye very nice :)
i wish i could find those basic tut on pHP in dutch without having to buy books :P

mony
09-17-2006, 09:41 PM
nice..thnx for the share :D

Wh1t3y
06-30-2007, 08:26 PM
Or if you wanted to echo "Hello World!" a bunch of times you could do this:

<?php
$text = "Hello World!";
echo $text
echo $text
echo $text
echo $text
?>

We defined the text variable as "Hello Word!"

So in your browser it would look like:

Hello World!
Hello World!
Hello World!
Hello World!

:)

SaIRoS
06-30-2007, 08:28 PM
it's basically the same -.-

Handbagstory
06-30-2007, 08:42 PM
Almost :P

Wh1t3y
06-30-2007, 11:06 PM
it's basically the same -.-[/b]
Yes, but it's another way of doing it

.azerone
09-02-2007, 10:22 PM
I don't get it.

griffinax
09-02-2007, 10:32 PM
^ lol

thnx for the tut

Dragonusthei
09-13-2007, 08:33 PM
I have never really found a use for functions in PHP. The language just does not require them.

Either go OOP or dont bother programming.

Vega
02-28-2008, 12:20 AM
^ completely agree

Go learn Classes kiddies, it makes programming fun and rewarding. Also enables easier editing. You'll love yourself!!