soltek Posted October 28, 2010 Share Posted October 28, 2010 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 =) Quote Link to comment https://forums.phpfreaks.com/topic/217100-rowy-or-rowy/ Share on other sites More sharing options...
PFMaBiSmAd Posted October 28, 2010 Share Posted October 28, 2010 $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. Quote Link to comment https://forums.phpfreaks.com/topic/217100-rowy-or-rowy/#findComment-1127564 Share on other sites More sharing options...
soltek Posted October 28, 2010 Author Share Posted October 28, 2010 $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! Quote Link to comment https://forums.phpfreaks.com/topic/217100-rowy-or-rowy/#findComment-1127568 Share on other sites More sharing options...
Psycho Posted October 28, 2010 Share Posted October 28, 2010 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". Quote Link to comment https://forums.phpfreaks.com/topic/217100-rowy-or-rowy/#findComment-1127570 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.