You are on page 1of 2

9/1/2021 Identifying Elements of an Array (Programming PHP)

home |
O'Reilly's CD bookshelfs |
FreeBSD | Linux | Cisco |
Cisco Exam
 

5.2. Identifying Elements of an Array


Ads by
You can
access specific values from an array using the array
variable's name, followed by the
element's key (sometimes
called the
index) within square brackets:
Send feedba
$age['Fred']
$shows[2] Tech

Why this ad? 

The key can be either a string or an integer. String values that are
equivalent to integer numbers (without leading zeros) are interview
treated as
integers. Thus, $array[3] and
$array['3'] reference the same element, but
$array['03'] references a different
element.
Negative numbers are valid keys, and they
don't specify positions from the end of the array as
they do in Perl. are hard
Akudemy -

You don't have to quote single-word


strings. For instance,
$age['Fred'] is the same as
$age[Fred]. However, it's Interview

considered good PHP style to always use quotes, because quoteless


keys are indistinguishable from
constants. When you use Coaching
Ads by a
constant as an unquoted index, PHP uses the value of the constant as
the index:
Joining Top tech
define('index',5); can be easy, wit
echo $array[index]; // retrieves $array[5], not $array['index']; focused

Send feedback preparations. W

Top tech

Why this ad? 


You must use quotes if
you're using interpolation to build the array index: guide you!

github.com
Interview
$age["Clone$number"]

However, don't quote the key if


you're interpolating an array lookup:
Coaching
// these are wrong
Akudemy -
print "Hello, $person['name']";
Interview
print "Hello, $person["name"]";
Coaching // this is right
print "Hello, $person[name]";
High pay,

OPEN
flexibility, safety,

and satisfaction,

often mean scary


5. Arrays 5.3. Storing Data in Arrays
tech interviews.

github.com

Copyright © 2003 O'Reilly & Associates. All rights reserved.

OPEN

Ads by
Send feedback Why this ad? 

Ads by
Send feedback Why this ad? 

https://docstore.mik.ua/orelly/webprog/php/ch05_02.htm 1/2
9/1/2021 Identifying Elements of an Array (Programming PHP)

https://docstore.mik.ua/orelly/webprog/php/ch05_02.htm 2/2

You might also like