Jump to content

$row[Y] or $row["Y"]


soltek

Recommended Posts

Hey there, this is a really tiny question (kinda noobish also) that i'd like you to help me with.

 

I read somewhere that $row[Y] and $row["Y"] would be processed at diferent speeds, is that right?

Could you explain to me the reason for that?

 

 

 

Thank you =)

Link to comment
Share on other sites

$row[Y] expects there to be a defined constant named Y. Php first looks in the table of defined constants to find what the constant Y equates to. When it does not find the constant Y, it invokes the error handler to address the non-existent constant. If you happen to have the error_reporting/display_errors/log_errors settings turned on, you will see an error message about the undefined constant.

 

Php than (in its brilliant wisdom) assumes that you meant to put quotes around the array index name and tries $row['Y']. It then does the normal lookup of the array index named 'Y', the same as if you have wrote the code correctly and put quotes around the array index name.

 

So, yes each time you leave off the quotes around an array index name, it probably takes at least 10x longer to reference that variable than if you had written the code correctly.

Link to comment
Share on other sites

$row[Y] expects there to be a defined constant named Y. Php first looks in the table of defined constants to find what the constant Y equates to. When it does not find the constant Y, it invokes the error handler to address the non-existent constant. If you happen to have the error_reporting/display_errors/log_errors settings turned on, you will see an error message about the undefined constant.

 

Php than (in its brilliant wisdom) assumes that you meant to put quotes around the array index name and tries $row['Y']. It then does the normal lookup of the array index named 'Y', the same as if you have wrote the code correctly and put quotes around the array index name.

 

So, yes each time you leave off the quotes around an array index name, it probably takes at least 10x longer to reference that variable than if you had written the code correctly.

 

Damn, it makes a lot of sense.

This tip is gonna help me a lot. Thank you, mate!

Link to comment
Share on other sites

Of course, you could have found this information yourself by simply reading the manual regarding arrays. http://php.net/manual/en/language.types.array.php

 

Array do's and don'ts

 

Why is $foo[bar] wrong?

Always use quotes around a string literal array index. For example, $foo['bar'] is correct, while $foo[bar] is not. But why? It is common to encounter this kind of syntax in old scripts:

<?php
$foo[bar] = 'enemy';
echo $foo[bar];
// etc
?> 

This is wrong, but it works. The reason is that this code has an undefined constant (bar) rather than a string ('bar' - notice the quotes). PHP may in future define constants which, unfortunately for such code, have the same name. It works because PHP automatically converts a bare string (an unquoted string which does not correspond to any known symbol) into a string which contains the bare string. For instance, if there is no defined constant named bar, then PHP will substitute in the string 'bar' and use that

 

Not only will it take a few milliseconds longer to process, the code could stop working entirely in future versions of PHP if they decide to fix that "bug".

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.