Web Develop Forum: Lesson 3 - Arrays, and functions - Web Develop Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Lesson 3 - Arrays, and functions In this lesson, Ill cover grouping data with arrays, and functions.

#1 User is offline   Jack Icon

  • Member
  • PipPip
  • Group: Teacher
  • Posts: 25
  • Joined: 31-January 09
  • Location:Australia

Post icon  Posted 18 February 2009 - 11:06 AM

Hi,

Sorry for the delay - I've been pushed for time for the last 4 days, and will be till the end of this week. But here is Lesson 3:

*Edit: I've rewritten this based on a few points made by Peter (my mistakes etc wink.gif)

Arrays:
These allow you to group adata into a sort of list. Its extremely useful for storing pairs of data, in the format of "key"="value".

<?php
$list = Array();
$list[] = "value1";
$list[] = "value2";
$list[] = "value3";
echo $list[2]; // displays "value3"
?>

Now, whats this [] you ask. Well its the way you call your data, better known as the index. Think of the Array as a sort of simple dictionary. You store the word in the key, and then definition in the value... The index can either be a string or an integer, if you try to use another data type, (dependign on the type) it will throw out an error ( index out of bounds from memory).

You can also define thes key=value pairs inside the Array() function itself:

<?php
$list = Array("key"=>"value");
$list["string1"] = "Hello World!";
?>

This will create the array:
$list["key"] = "value";
$list["string1"] = "Hello World!";

Now if you were to create an index (with a strign index) that did not use quotes... It will work, however (thanks Peter) it will try and find a constant with the same name first (I'll cover constants later on). As a general rule, you want to define keys with a string (usign quotes).

The value of an array can be anything you want, string, integer, float, MySQL object etc. Better still you can have it as an Array; its surprisingly useful to have an array of arrays. It creates a sort of grid... Think of storing a table of results etc.


Functions:
Functions are really good at doing "stuff". You tell the function what you want it to do, and with what data - and then you can use it wherever and whenever you want ( granted you have defined it in the script wink.gif).

For example:

<?php
//This function adds 2 numbers together
function add_numbers($number1, $number2) {
return $number1 + $number2;
}
echo add_numbers(4,5);
?>

As you can see, I define the function, and what I have to pass it. Then I tell it what to return. When a function returns a value, you can sort of think of it as the actual value of the function. * Note that it isnt the value, but its sort of like it*. In the end, its what the function evaluates as - so if you put it into an expression ( like I did when I called it on the last line above), thats what it equates to.

As with most variables, you can use functions to do anything to anything.... Its an alternative to copying and pasting code where you want to reuse it. Think of the above function.. (for exanple purposes only of course...) See if you wanted to repeat that code multiple times in your script, you could copy and paste it wherever you want it. However think what woudl happen if you accidently made a mistake without realisign it 0- you woudl have to find each location you copied it to and fix it manually. Instead you can run that code in a function - writign it only once, and then you call the function, which is much much much (etc) better!

Well thats all - ll put hyomework online tonight, and last weeks answers up soon.

Thanks for your patience!



0

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users