Simple Machines Community Forum

Archived Boards and Threads... => Archived Boards => Parham's PHP Tutorials => Topic started by: Parham on September 14, 2003, 10:54:55 AM

Title: PHP Lesson 12 - using functions
Post by: Parham on September 14, 2003, 10:54:55 AM
I've explained what a function is in lesson 3; now let me tell you how functions work from the PHP point of view.  You use functions when you have code that repeats.  Anytime you find yourself repeating a fair chunk of code, you can change it to a function saving yourself a lot of time typing, and a lot of space for the code.

PHP is built up of several functions.  For example, this function allows you to convert a string to upper case:


$string = 'this is a phrase';
$string = strtoupper($string);
echo $string; //prints "THIS IS A PHRASE";


strtoupper() is the function.  What it does is convert each character in my string to upper case characters.  This function saves you space because it automatically converts each character in the string to upper case for you.  If we look in the PHP documentation, this is what strtoupper() (http://ca3.php.net/manual/en/function.strtoupper.php) will tell us:

-The first line gives you the name of the function - "strtoupper"
-The second line tells us what versions of PHP handle this function - "(PHP 3, PHP 4)"
-The third line gives a very basic definition of what the function does - "strtoupper -- Make a string uppercase"
-The fourth line (and everything after) explains how the function works and gives definitions - "string strtoupper (string string)".

Let's take a look at how the function works:

string strtoupper (string string)

This says that when you use the "strtoupper" function and feed it a string, it will return a new string.  In other words: "string = strtoupper(string)".  You can read the PHP documentation on this function if you need examples.

The information you feed into a function are called "arguments".  The information a function can take are called "parameters".  These two words are sometimes used interchangeably.  When you define a function you define its parameters, but when you use a function, you give those parameters actual arguments.

Another very common function is the sort() function.  What this does is sort an array for you.  Let's look at the PHP documentation to see how to use this particular function (http://ca3.php.net/manual/en/function.sort.php).

void sort ( array array [, int sort_flags])

This function does not return any value (void), so you can't use assignment with this function.  In other words: "sort(array,int)".  The sort function takes an array as the first argument, and takes an optional second argument (that's what the [ and ] brackets mean, optional) which is an integer.  Reading the documentation a little more, we discover that the first argument is the array we want to sort, and the second optional argument allows us to sort the array in a particular way depending on the type of information we have in the array.  Let's look at a code example:


$array = array('b','a','q','z','y','a','c'); //let's define an array we want to sort
sort($array); //let's feed the array (via an argument) to the sort() function, and leave the optional argument blank
foreach ($array as $element) { echo "$element\n"; } //let's print out the array


That will print:

"a
a
b
c
q
y
z"

Read the PHP documentation and look for functions that might interest you.  Functions are named like actions (not quite verbs, but close).  You can find the functions (organized by type) at http://www.php.net/manual/en/funcref.php.  You can also find a full list of the functions at http://www.php.net/quickref.php.  To sum up everything above, you feed information to a function, it does a few things with the information you feed it, and it gives you new information back.
Title: Re: PHP Lesson 12 - using functions
Post by: pulpitfire on November 18, 2003, 10:02:13 AM
Thanks for the lessons.  It's really helping me understand.  I tried to visit these links, but they are no longer valid.
Title: Re: PHP Lesson 12 - using functions
Post by: Palli Moon on November 20, 2003, 04:30:49 PM
Pege, take the dot away and it will work...
Title: Re: PHP Lesson 12 - using functions
Post by: 6doubl5321 on February 07, 2006, 08:16:05 PM
i'm just now taking an interest in php as my new site is coded in it. i actually wanted to learn c++ as my first high-order languge (i'm adequate in intel/motorola assembly), but necessity dictates that i learn php first.:) to me, it seems that php is tougher than assembly, contrary to what 'they' say, lol.

anyway, i wanted to thank you for your effort! i particularly appreciate your pairing the examples with it's grammatical equivalent. i'll be back with questions, but please keep up the good work, bro!
Title: Re: PHP Lesson 12 - using functions
Post by: Parham on February 09, 2006, 08:40:02 AM
Thank you... they are't the best tutorials, but people seem to enjoy them.  I'm glad you're using them :).  I'm still here, but much less than before.  I do visit regularly though, so I'll still answer questions if you have any.
Title: Re: PHP Lesson 12 - using functions
Post by: 6doubl5321 on February 10, 2006, 03:25:46 PM
i assume php auto-alphabetizes arrays, but what would the code look like if we wanted to see your array in the order you coded it? (yes, i know coding it as html would be the best solution, but...)
Title: Re: PHP Lesson 12 - using functions
Post by: Parham on February 18, 2006, 06:33:43 PM
can you rephrase what you mean by that?  I don't think I quite understand your question.