Build Your Own DatabaseDriven Website Using PHP amp;amp; MySQL [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Build Your Own DatabaseDriven Website Using PHP amp;amp; MySQL [Electronic resources] - نسخه متنی

Kevin Yank

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید








Arrays

An array is a special kind of variable that contains
multiple values. If you think of a variable as a box that contains a value,
then an array can be thought of as a box with compartments, where each compartment
is able to store an individual value.

The simplest way to create an array in PHP is with the built-in array function:

$myarray = array('one', 2, 'three');

This code creates an array called $myarray that contains
three values: 'one', 2, and 'three'.
Just like an ordinary variable, each space in an array can contain any type
of value. In this case, the first and third spaces contain strings, while
the second contains a number.

To get at a value stored in an array, you need to know its index. Typically,
arrays use numbers, starting with zero, as indices to point to the values
they contain. That is, the first value (or element) of an array has index
0, the second has index 1, the third has index 2, and so on. In general, therefore,
the index of the nth element of an array is n-1.
Once you know the index of the value you're interested in, you can get that
value by placing the index in square brackets following the array variable
name:

echo($myarray[0]);       // Outputs "one"
echo($myarray[1]); // Outputs "2"
echo($myarray[2]); // Outputs "three"

You can also use the index in square brackets to create new elements,
or assign new values to existing array elements:

$myarray[1] = 'two';     // Assign a new value
$myarray[3] = 'four'; // Create a new element

You can add elements to the end of an array by using the assignment
operator as usual, except with empty square brackets following the variable
name:

$myarray[] = 'the fifth element';
echo($myarray[4]); // Outputs "the fifth element"

Array indices don't always have to be numbers; that is just the most
common choice. You can also use strings as indices to create what is called
an associative array. This type of array is called
associative because it associates values with meaningful indices. In this
example, we associate a date with each of three names:

$birthdays['Kevin'] = '1978-04-12';
$birthdays['Stephanie'] = '1980-05-16';
$birthdays['David'] = '1983-09-09';

Now if we want to know Kevin's birthday, we just look it up using the
name as the index:

echo('My birthday is: ' . $birthdays['Kevin']);

This type of array is especially important when it comes to user interaction
in PHP, as we'll see in the next section. I'll also demonstrate other uses
of arrays throughout this book.

/ 190