1
Coding & Programming / Basic PHP (Lesson 4)
« on: 12 Mar 2010 - 04:11 »
-Note, it really helps to have at least a basic knowledge of HTML as well as JavaScript before working with PHP, although its necessarily required, you are going to need to learn it eventually.
Hot damn! it's been a long time since the last tutorial hehe, sorry for the wait!
---------------------------------
Arrays are good for a lot of things in PHP, in fact there good for almost every situation where you need to create and use multiple variables. Lets say you have a list of chips,storing the chip types in single variables might look like this:
With an array,you can hold all your variable values under a single name. You can also access the values by referring to the array name, instead of having to create a whole new variable. Each little element within the array, has its own index which makes it way easier to be accessed.
Although there are only 3 types of arrays in PHP, all of them are useful.
The 3 types include:
How ever, that does absolutely nothing lol. So let's go ahead and make it do an action by adding an echo to the script.
The output of this would be:
First of all, lets explain this:
------------------Now your probably wondering
Q:"How the F*** is 2 going to be Cheetos when Doritos are clearly the second word?"
A:Well like i stated before the index starts at "0" so instead of "Fritos" being "1" the number is "0".
The output of this would be:
First of all, lets explain this:
Basically when storing data about specific named values, such as figuring out who bought which bag of chips,a numerical array is not always the best way to do it. So thankfully,with associative arrays we can just use the values as keys and assign values to them (crazy hehe!) and like Numeric Arrays they both have 2 methods that go along with them (oh happy joy >.> lol)
Now since this way isn't really all the cool and important (lul) i'll go over it quickly.
Example time!:
Let me explain this through comments so its easier to understand and compare.
Now that you are familiar with this method, lets make it do something!
Explanation of the newly added code through comments.
The "Is" and " a type of soda?" were both the beginning and end for what text we wanted displayed on each side of the value.
THUS THE OUTPUT WOULD BE!!! DUN DUN DUN!!!
Thus to yourself you can successfully say, "hell yeah its a soda and i know more php!" That's right yell it loud and proud!
*Not responsible if parents smack you for yelling :D*
Summary
In this tutorial we learned all the different types of arrays and how to use them. I really didn't expect the tutorial to come out with this amount of size, so instead of stressing everyone out, im just going to write another tutorial for Lesson 5 getting back on schedule with what i promised you guys.
Next lesson
While Loops and For Loops -I promise.
Contact
MSN - voxkilla@hotmail.com
Email - synysterray@gmail.com
Here on the forums.
Text. - friends and private users only.
Total Tutorial Time: 3 hours. Grrr
[Edited by Razor: Stickied]
Hot damn! it's been a long time since the last tutorial hehe, sorry for the wait!
---------------------------------
Lesson-4:What is an array?
Well arrays are very unique and wonderful things in the world of PHP. So what is an Array exactly? Well for starters, Arrays are Very Similar to Variables. As we know, variables are used as a storage area holding a number or text. However, a Variable only holds one value, and if your working with a great amount of items, variables aren't the way to go. This is where arrays come in. Unlike Variables, Arrays store multiple values in one single variable. You can almost refer to an array as a 'special variable' because it is far more advanced than a regular variable.Arrays are good for a lot of things in PHP, in fact there good for almost every situation where you need to create and use multiple variables. Lets say you have a list of chips,storing the chip types in single variables might look like this:
Code: [Select]
<?php
$chips1="Fritos";
$chips2="Doritos";
$chips3="Cheetos";
?>
Although this way works, what if you want to loop through a whole bunch of chip types and find a specific one? What if you had a list of over 200 brands of chips! The best solution in this situation would be to use an array.With an array,you can hold all your variable values under a single name. You can also access the values by referring to the array name, instead of having to create a whole new variable. Each little element within the array, has its own index which makes it way easier to be accessed.
Although there are only 3 types of arrays in PHP, all of them are useful.
The 3 types include:
Numeric array - An array with a numeric index.
Associative array - An array where each ID key is associated with a value.
Multidimensional array - An array containing one or more arrays.
Lets go over them shall we?Associative array - An array where each ID key is associated with a value.
Multidimensional array - An array containing one or more arrays.
Lesson-4:Numeric Arrays.
First lets begin with what an "Numeric Array" actually is. A Numeric Array stores each array element with a numeric index (I'll explain). Now since,there are only two methods to create a numeric array, its most smart to start off with the worse way to do it, although they both work fine.METHOD 1
Code: [Select]
<?php
$chips=array("Fritos","Doritos","Cheetos","Ruffles");
?> How ever, that does absolutely nothing lol. So let's go ahead and make it do an action by adding an echo to the script.
Code: [Select]
<?php
$chips=array("Fritos","Doritos","Cheetos","Ruffles");
echo "The Third bag of chips is ".$chips[2];
?> The output of this would be:
Code: [Select]
The Third bag of chips is Cheetos statsExplanation : METHOD 1
First of all, lets explain this:
Code: [Select]
$chips=array("Fritos","Doritos","Cheetos","Ruffles");This is a simple line of code, which sets all your variables within the array "$chips". Any of these can be accessed through a 'Numeric Code' which will be explained next.Code: [Select]
echo "The third bag of chips is ".$chips[2];This is a simple command. First we have the "echo" command which is the basic component for making our php script show our beautiful text. Since we used an array, (instead of something like "$chips2,$chips3," etc.) Because of that, we simply type the name of the array".$chips" and yes the 'dot' is important. Now the important part of this is the "[2]" the 2 represents the third variable within our array. The thrid variable happens to be "Cheetos".------------------Now your probably wondering
Q:"How the F*** is 2 going to be Cheetos when Doritos are clearly the second word?"
A:Well like i stated before the index starts at "0" so instead of "Fritos" being "1" the number is "0".
METHOD 2
Code: [Select]
<?php
$chips[0]="Fritos";
$chips[1]="Doritos";
$chips[2]="Cheetos";
$chips[3]="Ruffles";
echo $chips[2] . " and " . $chips[3] . " are really delicious.";
?>The output of this would be:
Code: [Select]
Cheetos and Ruffle are really delicious.Explanation : METHOD 2
First of all, lets explain this:
Code: [Select]
$chips[0]="Fritos";
$chips[1]="Doritos";
$chips[2]="Cheetos";
$chips[3]="Ruffles";This is alot like the other method, only in this one,we are manually assigning the numeric index to the variable.Both ways are effective but this one prevails for one big reason, that reason being the way easier ability to figure out the index number.Code: [Select]
echo $chips[2] . " and " . $chips[3] . " are really delicious.";This is a simple command. First we have the "echo" command which is the basic component for making our php script show our beautiful text. Much like the first version, we use "[NUMBER]" for defining a variable value. The text after the second to last quote, is the text to be displayed after the values are displayed.Lesson-4:Associative Arrays.
In an associative array, each ID key is associated with a value, which is kind of crazy because now you have an array,variable,index id, and a value to go along with all of it, sure gets confusing sometimes. Non the less, we can get through it :DBasically when storing data about specific named values, such as figuring out who bought which bag of chips,a numerical array is not always the best way to do it. So thankfully,with associative arrays we can just use the values as keys and assign values to them (crazy hehe!) and like Numeric Arrays they both have 2 methods that go along with them (oh happy joy >.> lol)
METHOD 1
Code: [Select]
<?php
$chips = array("Dane"=>Fritos, "Ray"=>Cheetos, "Lorenzo"=>Doritos, "Tyler"=>Ruffles);
?>Now since this way isn't really all the cool and important (lul) i'll go over it quickly.
Explanation : METHOD 1
[/color]Code: [Select]
$chips = array("Dane"=>Fritos, "Ray"=>Cheetos, "Lorenzo"=>Doritos, "Tyler"=>Ruffles);The things that make this code different than the numeric array are the way its set up. Before we used "Tyler" and that would be all we needed.But now were working with Associative Arrays so we need to add =>value, which COULD look like this:Code: [Select]
"PHP"=>FunMETHOD 2
Code: [Select]
<?php
$chips['Dane'] = "Fritos";
$chips['Ray'] = "Cheetos";
$chips['Lorenzo'] = "Doritos";
$chips['Tyler'] = "Ruffles";
echo "Ray purchased" . $chips['Ray'] . " from albertsons.";
?> Output of this would be:Code: [Select]
Ray purchased Cheetos from albertsons.Explanation : METHOD 2
Nothing really to explain here. I took Method 1, and just created it differently, instead i just manually added the index name instead of writing all of them on a single line.Lesson-4:Multidimensional Arrays.
Ok were down to the final type of array, the Multidimensional array.Basically in a multidimensional array, each element in the main array can also be an array, (like an array within an array make sense?). and each element in the sub-array can be an array, and it can go on and on and on and on etc.Example time!:
Code: [Select]
<?php
$Drinks = array
(
"Coke"=>array
(
"Sprite",
"Dr.Pepper",
"Root Beer."
),
"Tea"=>array
(
"Arizona Ice Tea"
"Green Tea"
),
"Other"=>array
(
"Powerade",
"Water",
"Apple Juice"
)
);
?> Let me explain this through comments so its easier to understand and compare.
Code: [Select]
<?php //start of your script
$Drinks = array //Beginning of Array
(
"Soda"=>array //Main-Array
(
"Coke", //Sub-Array
"Dr.Pepper", //Sub-Array
"Root Beer." //Sub-Array
), //acts as a divider.
"Tea"=>array //Main-Array
(
"Arizona Ice Tea" //Sub-Array
"Green Tea" //Sub-Array
), //acts as a divider.
"Other"=>array //Main-Array
(
"Powerade", //Sub-Array
"Water", //Sub-Array
"Apple Juice" //Sub-Array
)
); //Ends the list.
?> //end codeNow that you are familiar with this method, lets make it do something!
Code: [Select]
<?php
$Drinks = array
(
"Soda"=>array
(
"Coke",
"Dr.Pepper",
"Root Beer."
),
"Tea"=>array
(
"Arizona Ice Tea"
"Green Tea"
),
"Other"=>array
(
"Powerade",
"Water",
"Apple Juice"
)
);
echo "Is " . $Drinks['Soda][2] .
" a type of soda?";
?>
Explanation of the newly added code through comments.
Code: [Select]
<?php
$Drinks = array
(
"Soda"=>array
(
"Coke",
"Dr.Pepper",
"Root Beer."
),
"Tea"=>array
(
"Arizona Ice Tea"
"Green Tea"
),
"Other"=>array
(
"Powerade",
"Water",
"Apple Juice"
)
);
echo "Is " . $Drinks['Soda][2] . //Go's through the first variable Drinks,Then to Soda,and then 2, kind of like a chain of events.
" a type of soda?";
?>
The "Is" and " a type of soda?" were both the beginning and end for what text we wanted displayed on each side of the value.
THUS THE OUTPUT WOULD BE!!! DUN DUN DUN!!!
Code: [Select]
Is Root Beer a type of soda?Thus to yourself you can successfully say, "hell yeah its a soda and i know more php!" That's right yell it loud and proud!
*Not responsible if parents smack you for yelling :D*
END OF LESSON-4
Summary
In this tutorial we learned all the different types of arrays and how to use them. I really didn't expect the tutorial to come out with this amount of size, so instead of stressing everyone out, im just going to write another tutorial for Lesson 5 getting back on schedule with what i promised you guys.
Next lesson
While Loops and For Loops -I promise.
Contact
MSN - voxkilla@hotmail.com
Email - synysterray@gmail.com
Here on the forums.
Text. - friends and private users only.
Total Tutorial Time: 3 hours. Grrr
[Edited by Razor: Stickied]