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!

Syntax Question - Named keys with Quotes 3

Status
Not open for further replies.

sen5241b

IS-IT--Management
Sep 27, 2007
199
US
When using named keys sometimes I've noticed $SomeVar[name] and in other examples I've seen $SomeVar['name'] with the quotes. Is there any difference? It seems that both will work in the code.
 
it does matter. yes.

this
Code:
$array[key] = 'somevalue';

is wrong and will cause php to fire a notice. that slows down parse and execution time. php then guesses at what you might mean, which can cause undesirable results.

the rule is: associative array keys must ALWAYS be enquoted except when the occur within a double quoted string. For example this is the ONLY scenario where it is ok not to enquote strings

Code:
echo "This is the value of the associate array element: $array[somekey]";
 
Yeah but couldn't you use single quotes here?


Code:
echo "This is the value of the associate array element: $array['somekey']";
 
no. you should get a parse error if you try to do that.
Code:
[quote]
<b>Parse error</b>:  syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in ... [/quote]
 
or...
[tt] echo "This is the value of the associate array element: [blue]".$array['somekey'][/blue];[/tt]
 
thx, good info. I need to go back and add some quotes in my code!
 
i have seen (old) benchmarking studies showing that using single quotes and concatenation as opposed to heredoc or double quotes with inline expansion, is quickest.

not really on point but since tsuji posted a concatenation example, i thought i'd chip in.
 
Actually I find that interesting. But a well known programmer said "premature optimization is the root of all evil". I've been considering this whole issue lately.
 
i've long held that view about sql optimisation. and i guess code optimisation in general.

Consider this:

1. it costs 1000 USD/UKP for a high-level sql coder for a day. if you're lucky you will get a reasonable speed kick from a couple of days of analysis and tweaking.

2. for the same money you can buy a bigger processor/a second processor or even another box. you're almost certain to get a performance enhancement...


 
Apps running on a firewall, like spam filters, would need to be optimized. Also distributed intrusion detection systems, which propose to analyze every byte on the network, must be highly optimized.

I've worked with big industrial-size apps where throwing extra computing resources at the app helps at first but you reach a point where added CPU, memory and fast-IO starts giving you diminishing returns.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top