WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Array references - what -is- the authoritative way to do arrays?
Categories : Arrays, PHP Update Picture
Zeev Suraski
Date : Oct 12th 1998
Grade : 1 of 5 (graded 1 times)
Viewed : 8624
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Zeev Suraski
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 
 

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.



Single-file PHP news system with automatic folder structure creation
Categories : PHP, Filesystem, Arrays
Parsing html tags with php. Get an array from this function
Categories : PHP, HTML and PHP, Arrays, Tag Extractors
Takes an array and returns a string, suitable for inputing in an SQL statement
Categories : Arrays, Strings, PHP
Query2Report : Generating Html, Pdf and Csv Reports from SQL Query
Categories : PHP, PHP, HTML, PDF, Excel
Check for functional file links (broken Files)
Categories : PHP, Data Validation, FTP, Regexps, Arrays
Grab images from one or more URLs and save them to a specified local directory.
Categories : PHP, Filesystem, Strings, Arrays
How to Get a character array from a string
Categories : PHP, Strings, 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
This functions makes it easy to use session-variables known from ASP. With one Cookie the array "session" will save and restore from a db-record. In this version MySQL is used but it's should very easy to change
Categories : PHP, Arrays, Cookies, MySQL, Databases
Simple PHP Bar Graph using GD library
Categories : PHP, GD image library, Graphics, Arrays
A class to put get and post variables in hidden form elements. Works on scalars, normal arrays, associative arrays.
Categories : Algorithms, Variables, Arrays, PHP, PHP Classes
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
Function that returns an array with the 7 dates of the week that belong to the supplied date.
Categories : PHP, Date Time, Arrays
Human readable PHP password generator
Categories : PHP, Security, Beginner Guides, Arrays
How to pass an array to a function, or how to define a function wich recieves an array.
Categories : Variables, PHP, Arrays