Jul 26, 2008

Arrays In Php - My Fourth PHP Tutorial

Free Web Hosting, No Ads > CONTRIBUTE > Tutorials

free web hosting

Arrays In Php - My Fourth PHP Tutorial

ghostrider
This is the fourth part of my PHP tutorial. You can view the last three here:

Third Tutorial (Form Data and If statements)
Second Tutorial (Variables)
First Tutorial (What is PHP?)

Intro To PHP
Tutorial 4 - Arrays
Released 4/13/07
By Chris Feilbach aka GhostRider

Contact Info:
E-mail: assembler7@gmail.com
AIM: emptybinder78
Yahoo: drunkonmarshmellows
Website: http://www.ghostrider.trap17.com

Hello once again! During this tutorial I'm going to teach you about arrays. This tutorial does not have a project with it.

Remember back to my second tutorial, when you learned about variables. Variables allow programmers to store data, which then allows to do stuff with it. However variables can only store one value at a time. Arrays however, allow you to store more than one value at a time in the same variable, which makes arrays execeptionally useful in any langauge.

PHP has exactly 75 functions and statements that deal with arrays. They either sort them in different ways, tell you how many elements (values) are in them, or do a bunch of other stuff. We'll use maybe 3 or 4 of them in this tutorial.

Remember back to the last lesson when we learned about form data. The $_GET variable and the $_POST variable are arrays. They are called associative arrays because they have a key that is NOT a number. Arrays work very similar to form data in the sense that they two are in the (name=value) pair, meaning that each value has a name that allows you to access.

An element is a value stored in an array. Lets say we have the following web address:

http://www.ghostrider.trap17.com/index.php...s&submode=1

Lets get the GET data from this

CODE
$mode = $_GET['mode']; // $mode = tutorial
$submode = $_GET['submode']; // $submode = 1


Our keyes are mode and submode. In associative arrays they need to be put around single quotes. If you don't put single quotes around it you get an annoying message from PHP saying that you need to.

The second type of array is called a numerical array. In this case the keyes are integers, starting on whatever integer you want. Any floating number (a number with a decimal after it) will be truncated if you try to use it. Do yourself a favor and always use whole numbers. Unlike associative arrays, the key does not need to be put inside of single quotes.

While a numberical array can start on any number, I advise you to start on 0. Programmers start counting from 0, not 1. From now on you will always see me start my numerical arrays with 0. I highly suggest you do the same.

Arrays are created in one of two ways. You can simply define them like so:

CODE
$ourfirstarray['temp'] = 38593;
$ourfirstarray['name'] = "Chris";
$ourfirstarray['temp1'] = 88.55555;

$oursecondarray[0] = 27;
$oursecondarray[1] = "Hello";
$oursecondarray[2] = $ourfirstarray; // <-- Yes, you can do that in PHP.


Take a look at the last line of code. This now creates something special, something called a multidimensional array. These are very useful, but we won't be using that often yet. They are generally used when code becomes more complex than ours, and has more data to process. Multidimensional arrays can have as many dimensions as you want, but they take up a lot of memory. Use them sparingly and only if you feel you need to.

We can access the data in $ourfirstarray throught $oursecondarray

CODE
$temp = $oursecondarray[2]['temp']; // $temp = 38593
$temp = $oursecondarray[2]['name']; // $temp = Chris
// Be careful however
$temp = $ourfirstarray[2]['temp']; // $temp = NULL (nothing)


Just because one array is multidimensional does not mean the other array is.

There is another way of making an array. You can use the arrange function.

Lets create an array with the keys first and last with the values "Enter your first name." and "Enter your last name."

CODE
$array = array("first" => "Enter your first name.", "last" => "Enter your last name.");

print $array['first']; // Prints Enter your first name.


You can define as many key/value pairs as your wish using the array function. What about multidimensional arrays? You can create those, too.

CODE
$array = array("secondarray" => array("val1" => 22, "val2" => "Data"));

print $array['secondarray']['val1']; // Prints 22


You can also create numerical arrays using the array function. Do not put them in quotes. The array will then be considered associate if you do. Also, if you do not want to specify a new number everytime you define a key, you can specify a number, and then simply continue with printing the values, and not the keys. For example:

CODE
$array = array(0 => 1, 2, 3, 4);

print $array[0]; // Prints 1
print $array[1]; // Prints 2
print $array[2]; // Prints 3


This concludes this tutorial. Next tutorial we'll cover loops, and then the tutorial after that cookies. And after that, we'll start our first major project. Review a lot. You are at the point where you should know enough now to start to develop your own small scripts. Play around with it. If you develop something really cool, send me a link or code.

 

 

 


Reply

Laurie
I appreciate you sharing this course of tutorials. The thing I need that you dont provide is the idea of WHAT to program. Any one with suggestions as to possible concepts, please share them with me. I would like to practice using my programming skills.

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : arrays php fourth php

  1. Php - Randomize Web Title - using arrays and the random function (5)
    PHP - Randomize Web Title with the rand function... Define the variables numbers we start at number
    0 CODE <?php $title[0] = "Web title 0"; // Title 0
    $title[1] = "Web title 1"; // Title 1 $title[2] = "Web title
    2"; // Title 2 $title[3] = "Web title 3"; // Title 3
    $title[4] = "Web title 4"; // Title 4 ?> now we use the rand()
    function to get a randomize the web title. rand(0, 4) means we start at 0 and end at 4 i can get
    0,1,2,3,4 one of those. ...
  2. [php] Walking Through Multidimensional Arrays - Walk through multidimensional array with foreach and list (2)



Looking for arrays, php, fourth, php

Searching Video's for arrays, php, fourth, php
advertisement



Arrays In Php - My Fourth PHP Tutorial



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE