Function

20 11 2007

When you have a set of variables or want to make your code simpler, you will use a function. Creating a function is really easy all you have to do is write something like:

function myFunctionName() {
//Function data
}


Now if you have a look at the code above, where it says myFunctionName(), you can also add some parameters in between the ( ) brackets in case you want some special parameters to be used in the function.

Now we will take a short look at writing a simple function. In the following example, we will take a look at using a special parameter to write out a special string.

function writeString($string) {
echo $string;
}

Now, to call this function, you just use the very simple code:

writeString(“Hello, world!”);

Now that would return the following:

Hello, world!

Of course, you could also set a variable earlier on in the script and then just put $variable-name between the two brackets.

Of course, you could always add a more complicated things in the function, for example use other functions to reduce the amount of coding you have to do later on. Alternitavely, just jump on over to PEAR PHP and you’ll find loads of pre-written functions and classes so you don’t have to.

If you think I have missed anything out, just drop me a comment,

Thanks for reading,

Matt

About these ads

Actions

Information

2 responses

20 12 2007
axyner

nice, but why don’t you show an example in real code?

25 12 2007
Matt

“function writeString($string) {
echo $string;
}”

is an example of real code…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




Follow

Get every new post delivered to your Inbox.

%d bloggers like this: