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...