Mysterious Parse Error in Array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
RAM
Forum Newbie
Posts: 2
Joined: Tue Oct 08, 2002 11:34 pm

Mysterious Parse Error in Array

Post 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&#1111;"id"]="test";

$key="id";
print $record&#1111;'id'];	   # line 5
print $record&#1111;"id"];	   # line 6
echo "$record&#1111;$key]";     # line 7
echo "$record&#1111;'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
rev
Forum Commoner
Posts: 52
Joined: Wed Oct 02, 2002 3:58 pm
Location: Atlanta, GA

Re: Mysterious Parse Error in Array

Post by rev »

Change

Code: Select all

&lt;?php
echo "$record&#1111;'id']";	  # line 8
?&gt;
to

Code: Select all

&lt;?php
echo $record&#1111;'id'];	  # line 8
?&gt;
The quotes are causing the parse error.
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

Or if you want to do it within quotes

Code: Select all

&lt;?php

echo "{$record&#1111;'id']}";

?&gt;
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

or

Code: Select all

echo "$record&#1111;id]";
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

volka wrote:or

Code: Select all

echo "$record&#1111;id]";
Or not... as people have said before, php will look for a constant titled 'id'.. which slows down the script..
User avatar
RAM
Forum Newbie
Posts: 2
Joined: Tue Oct 08, 2002 11:34 pm

Thank You!

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

hob_goblin wrote:
volka wrote:or

Code: Select all

echo "$record&#1111;id]";
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) ;)
Post Reply