Page 1 of 1
Mysterious Parse Error in Array
Posted: Tue Oct 08, 2002 11:34 pm
by RAM
I'm having trouble with a parsing error in a small PHP script that pulls data from a database.
Code: Select all
<?php
$recordї"id"]="test";
$key="id";
print $recordї'id']; # line 5
print $recordї"id"]; # line 6
echo "$recordї$key]"; # line 7
echo "$recordї'id']"; # line 8
?>
This fails with the message: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' on line 8
Can anyone explain what I'm doing wrong?
-- RAM
Re: Mysterious Parse Error in Array
Posted: Wed Oct 09, 2002 12:51 am
by rev
Change
Code: Select all
<?php
echo "$recordї'id']"; # line 8
?>
to
Code: Select all
<?php
echo $recordї'id']; # line 8
?>
The quotes are causing the parse error.
Posted: Wed Oct 09, 2002 4:59 am
by mikeq
Or if you want to do it within quotes
Code: Select all
<?php
echo "{$recordї'id']}";
?>
Posted: Wed Oct 09, 2002 9:04 am
by volka
Posted: Wed Oct 09, 2002 5:10 pm
by hob_goblin
Or not... as people have said before, php will look for a constant titled 'id'.. which slows down the script..
Thank You!
Posted: Wed Oct 09, 2002 5:26 pm
by RAM
Many thanks for the help from everyone!
The example was stripped-down from a more complex program that was giving me fits. Since the echo statement in the real program contains string literals and I was counting on variable interpolation, rev's "quotless" solution won't help. (...but there's no way for him to know that from the example I posted).
Either the solution with the braces or the unquoted variable name as the hash key (despite the constant lookup speed penalty) will probably solve the problem nicely.
...you guys are great! Thanks again!
-- RAM
Posted: Wed Oct 09, 2002 6:51 pm
by volka
hob_goblin wrote:
Or not... as people have said before, php will look for a constant titled 'id'.. which slows down the script..
since we are already in a string-literal there's no need for the quotes. It really works, produces no errors or warnings and is consistent from php/zend's point of view
most times I (double-)check what I propose - if not I mark this fault ( or should go to bed)
