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!

appropriate syntax and why?

Status
Not open for further replies.

progman1010

Programmer
Jan 2, 2008
108
US
so an array var can be referenced several different ways. what is the purpose and does anyone know the current convention?

I used to use:
$array['keyname'] and within strings: "...{$array['keyname']}..."
but just recently I found that I could skip the quotes and make my life easier:
$array[keyname] and within strings: "...$array[keyname]..."

Why am i given the syntax option and is the first version just a legacy compatibility? I personally like the second way better....
 
Actually the second way should produce a Notice:
Code:
Notice: Use of undefined constant xxxx - assumed 'xxxx' in file.php on line 29

It only works because PHP is smart enough to assume you mean something else, however it can cause problems when you are actually using constants.




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Also it works when you have your PHP ini configured to show anything less than all Errors warnings and notices.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
so would you say that the second way is sloppy coding? (i actually saw its use for the first time within osTicket, an Open-Source support ticket system, and coincidentally is quite buggy)
 
do not skip the quotation marks for associative array keys other than as per below. it generates parser notices as vacunita says (which slows down your script). you also risk creating a collision with definitions. it is sloppy coding, a you infer.

the only time where it is acceptable NOT to enquote an associative array key is within double quotes.

Code:
echo "This is OK: $foo[bar]";

 
so in this case, would it be better to use "...$foo[bar]..." or "...{$foo['bar']}..." and why?
 
here are the rules:

1. $foo['bar'] or $foo["bar"] - use outside of enclosing double quotes.
2. use $foo[bar] when referencing the array insider enclosing double quotes

use curly braces to disambiguate arrays. typical use is inside a heredoc block

Code:
echo <<<HTML
 I am an array element: {$array['element']}
HTML;

the why is straightforward: because that's what the manual says ( the reason is that an unenquoted string literal is assumed to be a constant (defined term). php therefore searches through it's loaded definitions each time it comes across one of these. this takes time and processor cycles. then it throws an error (notice) - this takes time and processor cycles. Lastly it then makes ASSUMPTIONS about what you want: and that's never a good thing for a programmer.
 
Good stuff, guys. Thanks a lot. I like this explanation better than the one in the manual. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top