vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 General Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=111)
-   -   Why is $foo[bar] wrong? (https://vborg.vbsupport.ru/showthread.php?t=37203)

Sparkz 04-09-2002 09:21 AM

Why is $foo[bar] wrong?
 
I see this alot in hacks and the vb code in general, so I thought I'd quote the PHP manual:

Quote:

You might have seen the following syntax in old scripts:
PHP Code:

$foo[bar] = 'enemy';
echo 
$foo[bar];
// etc 

This is wrong, but it works. Then, why is it wrong? The reason is that, as stated in the syntax section, there must be an expression between the square brackets ('[' and ']').
That means that you can write things like this:
PHP Code:

echo $arrfoo(true) ]; 

This is an example of using a function return value as the array index. PHP knows also about constants, and you may have seen the E_* before.
PHP Code:

$error_descriptions[E_ERROR] = "A fatal error has occured";
$error_descriptions[E_WARNING] = "PHP issued a warning";
$error_descriptions[E_NOTICE] = "This is just an informal notice"

Note that E_ERROR is also a valid identifier, just like bar in the first example. But the last example is in fact the same as writing:
PHP Code:

$error_descriptions[1] = "A fatal error has occured";
$error_descriptions[2] = "PHP issued a warning";
$error_descriptions[8] = "This is just an informal notice"

because E_ERROR equals 1, etc.

Then, how is it possible that $foo[bar] works? It works, because bar is due to its syntax expected to be a constant expression. However, in this case no constant with the name bar exists. PHP now assumes that you meant bar literally, as the string "bar", but that you forgot to write the quotes.

So why is it bad then?
At some point in the future, the PHP team might want to add another constant or keyword, and then you get in trouble. For example, you already cannot use the words empty and default this way, since they are special keywords.

And, if these arguments don't help: this syntax is simply deprecated, and it might stop working some day.


Note: When you turn error_reporting to E_ALL, you will see that PHP generates warnings whenever this construct is used. This is also valid for other deprecated 'features'. (put the line error_reporting(E_ALL); in your script)

Note: Inside a double-quoted string, an other syntax is valid. See variable parsing in strings for more details.

Just thought I'd mention it...

alexp 04-19-2002 07:07 AM

its a handy way of replacing vars with their value within strings.

its quicker to go:

echo ("sometext $foo[bar] some more text");

than it is to go:

echo ("sometext ".$foo['bar']." some more text");

...but yes it is possible this might cause problems in the future with backwards compatibility - fair point.

Freddie Bingham 04-19-2002 04:10 PM

It's just lazy programming. vB3 has everything done properly, i.e.

$foo['bar'];
$foo["$bar"];

Admin 04-19-2002 05:33 PM

There is nothing wrong with using $foo[$bar] since $bar can never be a constant, and it's an expression. And in case you use a value from an array as an index for another array, using $post[$user['userid']] is better than $post["$user[userid]"] (since you can't do $post["$user['userid']"]).

Crazy Mofo 04-21-2002 01:58 AM

Firefly do you help programme vBulletin aswell?

Freddie Bingham 04-21-2002 02:21 AM

Well if $bar happens to be -1 than $foo[$bar] blows up if you are expecting -1 to be a string. $foo["$bar"] doesn't blow up.


All times are GMT. The time now is 04:43 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.02195 seconds
  • Memory Usage 1,740KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (4)bbcode_php_printable
  • (1)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (6)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete