Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

( ) and [ ] - When to and not to. Is there a rule? 2

Status
Not open for further replies.

josel

Programmer
Oct 16, 2001
716
US
After so many years of programming, I never developed the habit of using [ ] at all. At this point, I am wired to use ( ).

To this date, it has never played a role nor made any difference but as I start to work in PHP I wonder if time has come where this dog needs to learn a new trick.

As usual, your input will be greatly appreciated.

Regards,


Jose Lerebours


KNOWLEDGE: Something you can give away endlessly and gain more of it in the process! - Jose Lerebours
 
Hi

Jose said:
( ) and [ ] - When to and not to. Is there a rule?
Different meaning, no way to interchange them.
[ul]
[li]() encloses the parameter lists of functions and groups arithmetic expressions[/li]
[li][] encloses the indexes of arrays[/li]
[/ul]

Feherke.
 
Feherke,

Does this mean that arrays will not work if I used ( ) or that the preferred method is [ ]?

I love using arrays, which means I better get my pincky to work and learn a couple of new keys :)

I am totally new to PHP but I am sure I'll pick it up before the year is over :) :)

Thanks,


Jose Lerebours


KNOWLEDGE: Something you can give away endlessly and gain more of it in the process! - Jose Lerebours
 
square brackets designate array keys.

you can create arrays a number of ways

implicitly

Code:
$array[] = 'somevalue';
$array[] = 'someother value';

implicitly with associative keys

Code:
$array['one'] = 'somevalue';
$array['two'] = 'some other value';

or explicitly
Code:
$array = array('somevalue', 'some other value');

explicitly with associative keys
Code:
$array = array('one'=>'somevalue','two'=> 'some other value');
 
Hi

Jose said:
Does this mean that arrays will not work if I used ( ) or that the preferred method is [ ]?
To give you an explicit answer : no, array indexes will not work in () and no, it is not matter of preference.

In fact, no language comes in my mind where such thing is matter of preference...

Feherke.
 
jpadie,

Thanks so much for a well detailed explanation !!!

Feherke,

I have used 4GL programming tools where the use of ( ) are of no consequence to arrays.

--
Reading some of the code posted here and others I've read over the past few days, I can see where the use of [ ] simplifies things and helps identify functions vs arrays (specially in an environment where user-defined-functions are possible).

Noted, as originally posted by Feherke:
() encloses the parameter lists of functions and groups arithmetic expressions
[] encloses the indexes of arrays


Thank you both!


Jose Lerebours


KNOWLEDGE: Something you can give away endlessly and gain more of it in the process! - Jose Lerebours
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top