|
|
|
Here`s the deal.
First thing to realize is that PHP code (that is, code blocks inside PHP
tags) has two very distinct modes - regular code, and code inside double
quotes (there`s also code inside single quotes and backticks, but let`s
skip this for now).
With regular code, associative indices are any valid string expression.
That means a variable that contains a string, a string literal (e.g. "foo")
or some `complex` string expression like $a."foo" or the likes. Labels,
such as foo (without any surrounding quotes) are no longer considered valid
string literals (as of PHP 3.0RC5). Such labels are used for constants,
even though if you use foo, and foo isn`t a valid constant, the label would
be evaluated to the string expression "foo" (and would also generate a
notice-level warning).
To summarize, with regular code, you should use $array["foo"] instead of
$array[foo], even though both would work. If foo is a constant, the 2nd
format has a different meaning from the 1st (e.g. if foo==2, $array[foo] is
identical to $array[2]).
Inside quoted strings, PHP supports a much much more limited range of the
array variable references available in regular code. PHP 3.0 supports the
following formats inside quoted strings, nothing more, nothing less:
$a[7] 7 can be any valid number
$a[label] label can be any valid label, e.g. foo
$a[$b] $b can be any simple scalar variable
The following notation is also available to prevent certain ambiguities
under certain circumstances:
${a[7]}
${a[label]}
${a[$b]}
Now, the interesting part here is that when you want to access an
associative index inside a quoted string, you have only two options - use a
variable as an index (e.g. $b=="foo", $a[$b] == $a["foo"]), or, use a
label, like foo. Using "foo" explicitly will NOT work. Among other
things, this means that not all associative indices can be explicitly
accessed inside quoted strings (e.g., $array["foo bar"] cannot be directly
accessed inside quoted strings, unless you have a scalar variable that
contains the string "foo bar").
So, to extend the summary, inside quoted strings, you would normally access
associative arrays using $a[foo].
Future considerations
---------------------
I guess mostly everybody wants to write code that`ll be compatible with
future versions of PHP 3.x, namely version 3.1. None of us (core
developers) knows what exactly PHP 3.1 would look like, even though we have
plenty of ideas for it. Improving on the variable reference parsing inside
quoted strings is one of the things that`s scheduled for PHP 3.1. We
haven`t yet decided how the behavior would be exactly, but in order to
write safe code that would work under future PHP versions, it`d probably be
wise to follow the following guidelines for array references inside quoted
strings:
* For simple associative indices, use the simple $foo[bar] notation.
* Avoid using ${foo[bar]}. The general idea in PHP 3.1 is to enable
full-featured parsing inside the curly braces, which means foo would be
considered a constant rather than the string literal "foo".
* For `complex` array references, or ambiguous array references (i.e. ones
that require the ${foo[bar]} notation), use the concatenation operator,
e.g. "abcdefg" . $foo["bar"] . "hijklmnop" - this format will definitely
remain intact in future PHP versions.
A bit long, but I hope it helps.
|
|
| PHP Script to find url links in a page Categories : PHP, URLs, Regexps, Arrays | | | Looping through two arrays Categories : PHP, Databases, Arrays | | | Display list of files within current and subdirectories (recursively) showing
each file as an anchored link and each directory as a category header. Categories : Filesystem, Directories, Arrays, PHP | | | PHP Random rss feeds - selects 49 random feeds from an unlimited list and displays them on your website. It's Ideal for those moments when you got 5 minutes and dont know which one of your feeds to read. Categories : PHP, Rich Site Summary (RSS), Arrays | | | Simple way of scaling any image to fit either given width or height. Categories : PHP, Graphics, Arrays | | | PHP Dump in html format the contents of one array variable with a recursive list of the nested array variables inside. Categories : PHP, Arrays, Variables | | | A database abstraction layer for the PHP Oracle 8 module (available from PHP 3.0.5). It supports persistent connections, fetching rows into arrays, prepare/execute (variable binding) and has a new and improved error interface. Categories : Databases, Oracle, PHP, Arrays, Variables | | | CSS style switcher Categories : PHP, CSS, HTML and PHP, Arrays, Sessions | | | array -- Create an array Categories : PHP, PHP Functions, Arrays | | | Dynamic Loading of XML array data into ComboBox and Display XML data using PHP + DOM + Javascript. Categories : PHP, Java Script, DOM XML, XML, Arrays | | | Variable serialization and unserialization. Loading and saving variable structures
to and from file. Categories : Arrays, Filesystem, Variables, Strings, PHP | | | Tag content retrieval from websites with preg_match Categories : PHP, Regexps, Arrays, HTML and PHP | | | Array values from javascript to php Categories : PHP, Java Script, Arrays | | | How to get the source of a site into an array. Categories : Arrays, HTML, PHP | | | Weighted Random - Random Scripts usually chose one out of each item, and each item have an equal chance to be chosen. But what if you want an item to be chosed more frequently than other? Categories : PHP, Math., Arrays | |
|
|
|