ID:               15673
 Updated by:       [EMAIL PROTECTED]
-Summary:          Quotes crash MySQL queries: $array['val'] = Parse
                   Error, but  $array[val] works
 Reported By:      [EMAIL PROTECTED]
 Status:           Closed
 Bug Type:         Arrays related
 Operating System: Linux 2.4.5
 PHP Version:      4.1.1
 New Comment:

Constants are not looked for within strings, this is why "$foo[key]"
does not generate a warning here.  But outside of strings this is a
different story.

define ('a','b');
$arr = array('a' => 'apple', 'b' => 'banana');
print $arr['a'];    // apple
print $arr[a];      // banana
print "$arr[a]"     // apple
print "$arr['a']"   // parse error
print "{$arr['a']}" // apple

This bug report is a feature request for "$arr['a']" to print apple. 
Regarding print $arr[a] above, if 'a' was not a defined constant, a
warning would exist but apple is still printed (if key 'a' exists).


Previous Comments:
------------------------------------------------------------------------

[2002-02-22 11:44:45] [EMAIL PROTECTED]

> no warning is generated here as no define() substitution
> is done in strings
Why no define() is applied to the string??

>From the PHP manual:
[quote]Why is $foo[bar] wrong?[/quote]
[quote]...there must be an expression between the square brackets ('['
and ']').[/quote]

Since there is no expression, the workaround IS to make a define() to
the string or integer between the []s and set the value to its own
name. 

error_reporting(E_ALL);
define("ke","key");

$foo['key'] = 'aaa';
$foo['ke'] = 'bbb';
echo "<br>It is<br>"
        ."Foo[ke] ==> $foo[ke]";
echo "<br><br>It should be:<br>"
        ."Foo[ke] ==> ".$foo[ke];

Ok, we can deal with this, but thats not the way it should work
according with the good sense and the manual..

Regards

------------------------------------------------------------------------

[2002-02-22 10:31:22] [EMAIL PROTECTED]

> PHP is expected work with codes like:
> sql_query("SELECT * from $array['table']"); 

one *could* expect that it understood this,
that's what my feature request is about,
but "PHP is expected" is not right


> and its expected to (but doesnt have to) work with:
> sql_query("SELECT * from $array[table]");
> and in such case its expected to give a warning...

no, this is the current and expected behavior
no warning is generated here as no define() substitution
is done in strings

> in both cases the behavior of 4.1.1 is buggy...
it has been like this ever since 3.0, so you can't
call this 'buggy', call it 'unexpected  behaviour',
'inconsistent design' or whatever, but it is not
a bug ...




------------------------------------------------------------------------

[2002-02-22 10:02:10] [EMAIL PROTECTED]

Ok, but I think it should be seen as a BUG, since we cannot call the
array as it should be and we are able to call it as it shouldn't.

PHP is expected work with codes like:
sql_query("SELECT * from $array['table']"); 

and its expected to (but doesnt have to) work with:
sql_query("SELECT * from $array[table]");
and in such case its expected to give a warning...

in both cases the behavior of 4.1.1 is buggy...

Thanks for your time people!
Best Regards

Joćo Paulo M. Fischer

------------------------------------------------------------------------

[2002-02-22 09:42:58] [EMAIL PROTECTED]

i'll close this here and create a feature request
for "$array['index']" to be possible or for a better
error message at least ...

see http://bugs.php.net/15677


------------------------------------------------------------------------

[2002-02-22 08:40:58] [EMAIL PROTECTED]

And the Parse Error, do you get it with the cvs ver too?

------------------------------------------------------------------------

The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
    http://bugs.php.net/15673

-- 
Edit this bug report at http://bugs.php.net/?id=15673&edit=1

Reply via email to