473,385 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Arrays: $SomeArray{0} vs. $SomeArray[0]

I have found that in PHP 5, <?php $SomeArray{0} = $somevalue ?(note
the squigly braces) appears to be functionally equivalent to the more
usual <?php $SomeArray[0] = $somevalue ?(square array brackets).

However, doing $SomeArray{} = $somevalue raises an error.

I can't seem to find any documentation that indicates why you can use
squigly braces in the case where an array index is provided. Was
wondering if someone could explain why they work in that situation, and
possibly provide a reference to this use of squigly braces in the PHP 5
documentation.

(I am a rank PHP newbie, for whatever that's worth..)

Dec 1 '06 #1
5 1259
"Ira Gladnick" <Ir*********@yahoo.comwrote in message
news:11**********************@n67g2000cwd.googlegr oups.com...
I have found that in PHP 5, <?php $SomeArray{0} = $somevalue ?(note
the squigly braces) appears to be functionally equivalent to the more
usual <?php $SomeArray[0] = $somevalue ?(square array brackets).

However, doing $SomeArray{} = $somevalue raises an error.

I can't seem to find any documentation that indicates why you can use
squigly braces in the case where an array index is provided. Was
wondering if someone could explain why they work in that situation, and
possibly provide a reference to this use of squigly braces in the PHP 5
documentation.

(I am a rank PHP newbie, for whatever that's worth..)
You use [ ] on arrays such as:

$someArray[0] = "0123456789";
$someArray[1] = "rock 'n roll";

echo $someArray[0] will output:
0123456789

echo $someArray[1] will output:
rock 'n roll

You use { } on strings such as:

$someString1 = "0123456789";
$someString2 = "rock 'n roll";

echo $someString1{5} will output:
5

echo $someString2{3} will output:
k

strings are zero based... with this method you can manipulate strings at the
character level if you want.

Norm
--
FREE Avatar hosting at www.easyavatar.com
Dec 1 '06 #2
..oO(Norman Peelman)
>You use [ ] on arrays such as:
[...]

You use { } on strings such as:
[...]
You can also use [] on strings, even if it was not recommended in older
PHP versions. The {} syntax is now deprecated as of PHP 5.1 and will be
removed in PHP 6.

4.5 Cleanup for {} vs. []
http://www.php.net/~derick/meeting-n...cleanup-for-vs

Micha
Dec 1 '06 #3

Norman Peelman wrote:
You use { } on strings such as:

$someString1 = "0123456789";
$someString2 = "rock 'n roll";

echo $someString1{5} will output:
5
Interesting.

But I was actually doing an assignment using the brace notation:
SomeArray{0} = $somevalue

And, when I did a print_r($SomeArray), it displayed as being an array
with the zero element being the assigned value (if I change the
assignment to SomeArray{99} = $somevalue, then it shows the 99th array
element as holding $somevalue).

So in this situation it seems that braces are equivalent to array
brackets, and are doing something different than string indexing. But
as mentioned, I can't seem to find any documentation indicating this
usage.

This kind of brace usage came up in the context of a certification
practice exam.

Dec 1 '06 #4
Thats exactly what I mean. every tutorial, book seems to indicate different
things.
"Ira Gladnick" <Ir*********@yahoo.comwrote in message
news:11**********************@l12g2000cwl.googlegr oups.com...
>
Norman Peelman wrote:
>You use { } on strings such as:

$someString1 = "0123456789";
$someString2 = "rock 'n roll";

echo $someString1{5} will output:
5

Interesting.

But I was actually doing an assignment using the brace notation:
SomeArray{0} = $somevalue

And, when I did a print_r($SomeArray), it displayed as being an array
with the zero element being the assigned value (if I change the
assignment to SomeArray{99} = $somevalue, then it shows the 99th array
element as holding $somevalue).

So in this situation it seems that braces are equivalent to array
brackets, and are doing something different than string indexing. But
as mentioned, I can't seem to find any documentation indicating this
usage.

This kind of brace usage came up in the context of a certification
practice exam.

Dec 2 '06 #5
Ira Gladnick wrote:
Norman Peelman wrote:
>>You use { } on strings such as:

$someString1 = "0123456789";
$someString2 = "rock 'n roll";

echo $someString1{5} will output:
5


Interesting.

But I was actually doing an assignment using the brace notation:
SomeArray{0} = $somevalue

And, when I did a print_r($SomeArray), it displayed as being an array
with the zero element being the assigned value (if I change the
assignment to SomeArray{99} = $somevalue, then it shows the 99th array
element as holding $somevalue).

So in this situation it seems that braces are equivalent to array
brackets, and are doing something different than string indexing. But
as mentioned, I can't seem to find any documentation indicating this
usage.

This kind of brace usage came up in the context of a certification
practice exam.
I don't think this was ever legal for arrays - just something which
wasn't properly caught. At least I've never seen this syntax for arrays
in any of the official PHP doc.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Dec 2 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
5
by: jeremy targett | last post by:
Hello, in my program I use 2-dimensional arrays to hold two integer values for each of i members of a list. array holds a starting position, and array holds a label - in fact I #defined START as 0...
5
by: JezB | last post by:
What's the easiest way to concatenate arrays ? For example, I want a list of files that match one of 3 search patterns, so I need something like DirectoryInfo ld = new DirectoryInfo(searchDir);...
3
by: Michel Rouzic | last post by:
It's the first time I try using structs, and I'm getting confused with it and can't make it work properly I firstly define the structure by this : typedef struct { char *l1; int *l2; int Nval; }...
1
by: Nathan Gilbert | last post by:
I have a function that is returning a 2D array (declared using double pointers) and I want to be able to append a row of data to the beginning and end of this array without having to create a new...
1
by: Rob Griffiths | last post by:
Can anyone explain to me the difference between an element type and a component type? In the java literature, arrays are said to have component types, whereas collections from the Collections...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
1
by: Doug_J_W | last post by:
I have a Visual Basic (2005) project that contains around twenty embedded text files as resources. The text files contain two columns of real numbers that are separated by tab deliminator, and are...
16
by: mike3 | last post by:
(I'm xposting this to both comp.lang.c++ and comp.os.ms- windows.programmer.win32 since there's Windows material in here as well as questions related to standard C++. Not sure how that'd go over...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.