Search
Left Quote    I start where the last man left off.
- Thomas A. Edison    
Right Quote
 
[login] | [Register]
 

Introduction to PHP

by: Jester
Page: 3 of 7
(View All)

In this section we’ll look at arrays, how we use them, why we use them and we’ll take a look at a few special arrays available to you in PHP scripts.

Arrays

Arrays in PHP are just what they say, arrays of variables. Let’s take a look at what one would look like.



An array is accessed by the name of the array, followed by square brackets containing the key of the array item we want to access.

$array[key]

An array contains an array of values, each of which are referenced by this “key”. We use the keys to access each individual item within the array. We would assign an array like this:



All that information would be stuck into the $array array. We could then access each item:



The key index consists of numbers, the first value in the array is accessed with 0, NOT 1. The second accessed by 1, the third by 2, and so forth.

Associative Arrays

You don’t have to access array items with number values starting at 0, you can define your own keys.



That may look a bit daunting, but just look at it. Each array key => item is defined separated by commas. The part before the => is the key and the part after it is the value associated with that key.



Associative arrays allow you to associate different words with each array item, it is easier to remember which word accesses what item in large array, your mind rememebrs words easier than having to count, starting from zero, in your head.

Remember the single quotes/double quotes thing? This applies when accessing array items with keys also. An integer value or a variable needs not be quoted, a string value needs aingle quotes, a string value containing a variable needs double quotes.

Special Arrays

$_SERVER

This array contains lots of values for you to use in your scripts. It contains the client’s IP address, the browser version, the URI of the script being access, various HTTP headers and much more, here are some examples.



$_POST

This array contains anything that has been submitted through the POST method. We will be looking at this in the next chapter.

$_GET

This array contains anything that has been submitted through the GET method. We will be looking at this in the next chapter.

$_COOKIE

Contains cookie information for the client. Say you set a cookie on their machine named “mycookie” with a value of “Jester”. $_COOKIE['mycookie'] would contain 'Jester'.

$_REQUEST

This associative array consists of everything in $_POST, $_GET, $_COOKIE and $_FILE.

For more information see The PHP Manual.

In the next chapter we’ll discuss using forms with PHP scripts and how we process the information submitted.



1  |  2  |  3  |  4  |  5  |  6  |  7  |  
Next »


No Comments for this page.

You Must be logged in or a member to comment.


Tutorial Stats

Tutorial Stats

320,998 Views
9 Total Comments
5 Rating of 5 (2 Votes)

Options

Tutorial Options

· Login to Rate This Article
· Login to Post a Comment
· Read more by this author
Digg This Article! Del.icio.us: Bookmark This Article Reddit: Bookmark This Article BlinkList: Blink This Article! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Article Spurl: Mark This Article

Articles

Related    

· Simple PHP Tutorial
· One File Website
· Pagination with PHP
· Building a Comments Script
· Complete Members System
· Variable Scope



"AllSyntax.com" Copyright © 2002-2017; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank