00:00
00:00
Newgrounds Background Image Theme

cementtt just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Php: Functions & Arrays

2,571 Views | 6 Replies
New Topic Respond to this Topic

Php: Functions & Arrays 2006-08-01 16:01:44


T3h PHP: Main -- Shut up, Sit down, Click this link, and don't ask any more questions about PEE-AYCH-PEE

Fucntions, Array's
What you will be learning?

You will be learning about PHP's Functions, and Arrays

Who does this apply to?

This tutorial is applyed to (should be watched by) someone who has at least a basic knowledge of php. E.G. you should know how to <?php echo ?> stuff and other simples like that.

<---Moving on--->

To start, i'll Teach you about Arrays. Then we'll move on from there.

Arrays are just simple ways of being lazy. They do come in handy when you have to re-write 1-100 over and over...

Arrays are commonly grouped with the foreach loop. But we'll get to that later.

------

Ok, explaining done for arrays. Now, open a blank .php and name it random.php (doesn't matter what you name it)

Let's start with an easy array.
Here's an example of an array:

$people = array ('Joe', 'Bob', 'Carlita', 'Joobie');

The $people variable now has the value of JoeBobCarlitaJoobie.
Yes, i know, it's not amazing, but that will come in the future, which is about now.

Another way of writing arrays is to use the '=>' sign.

$people = array ('Joe' => 'male', 'Bob' => 'male', 'Carlita' => 'female', 'Joobie' => 'bi');

The genders now have 'key's that php can go off of. The key is simply a number or a letter (or more than one) that is linked to something. Like '1' would be linked to 'Janurary' because Janurary is the first month of the year.
There is a couple more types of arrays you can write and i'll show you how to do before we get into displaying them.

The next way of writing them is to use a number as the key.

$days = array (1 => 'Sunday', 2 => 'Monday', 3 => 'Wednesday');

Now, you may or may have not noticed that I didn't put the numbers in quotes. There is no need. PHP doesn't require it.

And easier way of writing the array above is

$days = array (1 => 'Sunday', 'Monday', 'Wednesday', 'Friday');

This will automatically set the number 1 to Sunday (as it's key) and the number 1 will increase every time a new word is placed. So monday has a value of 2 now. And Wednesday is 3 ect.
That method is highly used when referring to numbers and increasing numbers with values. But, that method only works with numbers and the numerical alphabet ( a, b, c, d, ect.)
So you could replace the number 1 with the letter 'n' and it will do the same thing, but with letters.
It doesn't work with words. Don't try it, you'll just get an error.

Another way of writing arays is to write them out one by one, but i'd stick with the methods above if i were you, but this does come in handy sometimes.

$array[] = 'John';;
$array[] = 'Moby';
$array[] = 'Penis';

To echo this type of array is simple. But like all arrays, the number value always starts at 0 unless else noted.

echo $array[1];

That would give you the output of Moby

There is another type of array, it's used for numbers.

$year = range (2005, 2020);

This will output the number 2005, 2006, 2007, ect. all the way up to 2020.

====

Now that you know how to write an array, let's start echoing them onto the php page.

We'll use the first script I showed you:
$people = array ('Joe', 'Bob', 'Carlita', 'Joobie');

To write it out, I'll show you how to use a foreach loop. The foreach loop looks something like this:

foreach ($people as $value) {
// Do something special with the variable $value
}

This takes the array '$people' and does something with it's values (or the people listed inside the array, aka joe, bob, carlita, and joobie)
Now you don't have to use $value or $key as i am about to show you, but it's just an easy way of recognizing it. So i suggest just use $value instead of some other fancy shmancy variable.

So, let's do a simple loop.

foreach ($people as $key => $value) {
echo "Hello $value! Your number is $key!";
}

This will just echo this: Hello Joe! Your number is 0! and so on and so forth.
the $key is the $value's number. So, since Joe is the first one listed in the array, his $key is number 0. Since php starts numerically at 0 and not one, this will always happen unless you specify otherwords.

Let's move on the the next array that i showed you.
$people = array ('Joe' => 'male', 'Bob' => 'male', 'Carlita' => 'female', 'Joobie' => 'bi');

Now, this array has keys. Joe, is the key for male. But since we have more than one male, it might cause overwrites, so we don't want that. Let's change it to something different.
$people = array ('Joe' => 'male', 'Bob' => 'penis', 'Carlita' => 'female', 'Joobie' => 'bi');

There, now with a foreach loop, let's see how to write it...

foreach ($people as $key => $value) {
echo "Wow! $key is a $value!";
}

This will echo: Wow! Joe is a male! and so on and so forth
The next array we have is:
$days = array (1 => 'Sunday', 2 => 'Monday', 3 => 'Wednesday');

Let's write that in a foreach loop.

foreach ($days as $key => $value){
echo "Ok, let's see, so the number $key has to do with $value..";
}

This will echo: Ok, let's see, so the number 1 has to do with Sunday.. ect, ect...

And our next array is..:
$days = array (1 => 'Sunday', 'Monday', 'Wednesday', 'Friday');
I'm not going to write this out in a foreach loop because the example above does the exact same this, it's just that this array has less to write. :)

Our next array is:
$array[] = 'John';;
$array[] = 'Moby';
$array[] = 'Penis';

To write it out in a foreach it would look something like this:
foreach ($array ask $key => $value) {
echo "Well, well, well, it looks like $value is number $key!";
}

If you haven't gotten it yet, the key is just a number and the value is the name or word that is in the quote marks after the equal sign. Like i said, if this is the first php tutorial you are looking at, you might want to learn other php first.

And the final array is:
$year = range (2005, 2020);

To write it out would be:
foreach ($year as $value) {
echo 'There year is now $value";
}

CONT...


Hello, from the past!

[ PHP: Main ]

BBS Signature

Response to Php: Functions & Arrays 2006-08-01 16:03:16


-------------
...CONTINUED

There are more complex ways of writing arrays. But, let's put the complex ones in functions! Yea functions!

A function is just a simple "store" to hold things. Kind of like a database, but much less big.

So, a function basically consists of this:

function FUNCTION_NAME () {
// function code
}

simple right? Well, i'll let you decided.
The FUNCTION_NAME can only have alphanumerical characters (0-9, a-z) and can have underscores '_' but it must start with a letter or an underscore.

Functions are not case-sensitve so funCtIon_nAmE and FUNCTION_NAME are the same thing.

Let's start with a simple function.

function random ($m = NULL) {
$month = array (1 => 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec');

// now put that array in a drop down menu!
echo '<select name="month">;
foreach ($month as $key => $value) {
echo "<option value=\"$key\";
if ($key == $m){
echo ' selected="selected"';
}
echo ">$value</option>";
}
echo '</select>'

Now you may have noticed the if statement in there. Since, in the function's paren. '()'
We defined $m as being equal to NULL or FALSE or not true or empty or blank.
So, if the $key (the number) is blank, or NULL or empty, it will echo selected="selected"

Which means that if a user selects that particular item or month. When he hit's enter, his data will be "saved" somewhat. So, if there is an error in the form, when he goes back, his choice will still be selected, so he won't have to redo the whole thing.

To call a function, or to place it on the php document, you must first have the actual function in the same .php file as you are trying to call it. Which means, that this function must be in random.php to work.

If you are still in random.php (which you should be) you can call this function like this:

random();

This code will then put the function 'random' in the place of the random(); code.

As you can see, you can define things in the funtion's '()' like $pee = poop or something like that. to separate them, just use a comma (,). Or you could just name the variables, as shown in the below example.

function hello_world ($hello, $goodbye, $world) {
echo "$hello -- $world -- and -- $goodbye!";
}

Then to call it up and use it, just do this.

hello_world(blue, yellow, china);

This will then appear:

Blue -- China -- and -- Yellow

======

As you can see, functions have a big role. You can use them instead of writing the same code over and over, you could just write the function name over and over.

The things I taught you today about functions are just the start. There are many possiblities. The stuff i just taught you about replaceing variables inside the '()' is a wide subject, but that is the basics. You can get very complex with functions and arrays.

But to get complex, it is needed to get simple. Complex is just a lot of simple, so it's not that hard if you use common sense, but for some of you, it's hard isn't it? If you can't use common sense, you can't use php. And you can't use web design, so just quit now.

If you actually want to try hard, learn to use common sense, then you'll achive great things in life, like learning how to use functions and arrays together in complex forms, by just using this tutorial alone.

===

Ok, that's my speil about Funcitons and Arrays. I hope someone learned something someone or my job is not done. I hope you all enjoyed this tutorial. And if everyone (or most people) like it, then maybe i'll make another one about For's and While's or something like that.
I know it’s a lot to read, but if you are just reading this part, and not the whole tutorial, then I suggest not ever reading anything. Unless you are already know arrays and functions like the back of your hand, I would suggest you stop being like any other n00b in the forum and actually read something.

For those of you who did read it, Thanks for reading!

As for now, Momo has to go potty Buh-Bye!


Hello, from the past!

[ PHP: Main ]

BBS Signature

Response to Php: Functions & Arrays 2008-01-29 19:07:54


Wow... I am the only reply... Great job.

Response to Php: Functions & Arrays 2008-01-30 03:32:13


Very nice, but you missed a few things.

On arrays
You can use a handy short syntax for adding array elements with custom keys.
Say I have this loop:

$array = array();
for($i = 0; $i != 10; $i+=2)
    $array[$i] = 'Hello!';

This'll give an array like this:

0 => 'Hello!',
2 => 'Hello!',
4 => 'Hello!',
6 => 'Hello!',
8 => 'Hello!',

Which can be very handy!
There are also a whole lot of handy array functions.

On functions
You missed out on default arguments and a variable number of arguments ;)

Oh, and use code tags.


"My software never has bugs. It just develops random features. " - Unknown

[ FluxBB developer | Quickmarks 0.5.1 | Strings & Ints - my blog ]

BBS Signature

Response to Php: Functions & Arrays 2008-01-30 03:54:10


At 1/30/08 03:32 AM, elbekko wrote: Very nice, but you missed a few things.

i think im just more disappointed in the descriptions of things

At 8/1/06 04:01 PM, Momo-the-Monkey wrote: Arrays are just simple ways of being lazy. They do come in handy when you have to re-write 1-100 over and over...

i think what you meant to say is that arrays are data elements stored contiguously (in any real language) in memory, by convention having the same datatype. in PHP, however, arrays are dynamic width, and operate in a linked list manner, with each element "pointing" to the next element in the list.


BBS Signature

Response to Php: Functions & Arrays 2008-01-30 06:39:54


Uh, thanks for the comments, but you guys are a little late. Check the date. Also, I couldn't have used code tags when I made this post. They weren't available at the time.

Sorry for not being perfect :p

Hello, from the past!

[ PHP: Main ]

BBS Signature

Response to Php: Functions & Arrays 2008-01-30 06:43:21


Oops, thought it was a new one - being unread and all :P


"My software never has bugs. It just develops random features. " - Unknown

[ FluxBB developer | Quickmarks 0.5.1 | Strings & Ints - my blog ]

BBS Signature