Connecting Tech Pros Worldwide Forums | Help | Site Map

heredocs and numbers in indexes

Jeff
Guest
 
Posts: n/a
#1: Nov 15 '08
I have an array;

$array['1a'] = 'something';
$array['a1'] = 'something else';

I can do this: echo $array['1a'];

And I can do this:

$content =<<<content

$array[a1]

content;

But if the index starts with a digit, php fails looking for the trailing
"]".

$content =<<<content

$array[1a]

content;

Why is that? Is there a fix other than renaming the indexes?
{$array[1a]} or {$array['1a']} does not work.

Is this a bug or is it a feature I don't understand?

Jeff

Thomas Mlynarczyk
Guest
 
Posts: n/a
#2: Nov 15 '08

re: heredocs and numbers in indexes


Jeff schrieb:
Quote:
$content =<<<content
$array[1a]
content;
Quote:
Why is that? Is there a fix other than renaming the indexes?
{$array[1a]} or {$array['1a']} does not work.
{$array['1a']} works fine for me with PHP 5.2.0.

Greetings,
Thomas

--
Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)
Jeff
Guest
 
Posts: n/a
#3: Nov 15 '08

re: heredocs and numbers in indexes


Thomas Mlynarczyk wrote:
Quote:
Jeff schrieb:
>
Quote:
>$content =<<<content
>$array[1a]
>content;
>
Quote:
>Why is that? Is there a fix other than renaming the indexes?
>{$array[1a]} or {$array['1a']} does not work.
>
{$array['1a']} works fine for me with PHP 5.2.0.
Thanks, I've got 5.2.6 here.

I've also some weird problem where with $ signs.

$D['some_var']= '$19.99';

echo <<<content
$D[some_var]
content;

yields this: .99

Just looked at the php change log but it's a bit hard to parse!

Jeff
Quote:
>
Greetings,
Thomas
>
Luuk
Guest
 
Posts: n/a
#4: Nov 15 '08

re: heredocs and numbers in indexes


Jeff schreef:
Quote:
Thomas Mlynarczyk wrote:
Quote:
>Jeff schrieb:
>>
Quote:
>>$content =<<<content
>>$array[1a]
>>content;
>>
Quote:
>>Why is that? Is there a fix other than renaming the indexes?
>>{$array[1a]} or {$array['1a']} does not work.
>>
>{$array['1a']} works fine for me with PHP 5.2.0.
>
Thanks, I've got 5.2.6 here.
>
I've also some weird problem where with $ signs.
>
$D['some_var']= '$19.99';
>
echo <<<content
$D[some_var]
content;
>
yields this: .99
>
Just looked at the php change log but it's a bit hard to parse!
>
Jeff
Quote:
>>
>Greetings,
>Thomas
>>
what OS are you running on ?
Jeff
Guest
 
Posts: n/a
#5: Nov 15 '08

re: heredocs and numbers in indexes


Luuk wrote:
Quote:
Jeff schreef:
Quote:
>Thomas Mlynarczyk wrote:
Quote:
>>Jeff schrieb:
>>>
>>>$content =<<<content
>>>$array[1a]
>>>content;
>>>
>>>Why is that? Is there a fix other than renaming the indexes?
>>>{$array[1a]} or {$array['1a']} does not work.
>>>
>>{$array['1a']} works fine for me with PHP 5.2.0.
>>
> Thanks, I've got 5.2.6 here.
>>
> I've also some weird problem where with $ signs.
>>
>$D['some_var']= '$19.99';
>>
>echo <<<content
>$D[some_var]
>content;
>>
>yields this: .99
>>
>Just looked at the php change log but it's a bit hard to parse!
>>
> Jeff
Quote:
>>>
>>Greetings,
>>Thomas
>>>
>
what OS are you running on ?
some flavor *nix.

head litenup.ca ?

It's a Blue Genesis server, not a great host, if you ask me!

Jeff
Jerry Stuckle
Guest
 
Posts: n/a
#6: Nov 15 '08

re: heredocs and numbers in indexes


Jeff wrote:
Quote:
I have an array;
>
$array['1a'] = 'something';
$array['a1'] = 'something else';
>
I can do this: echo $array['1a'];
>
And I can do this:
>
$content =<<<content
>
$array[a1]
>
content;
>
But if the index starts with a digit, php fails looking for the trailing
"]".
>
$content =<<<content
>
$array[1a]
>
content;
>
Why is that? Is there a fix other than renaming the indexes?
{$array[1a]} or {$array['1a']} does not work.
>
Is this a bug or is it a feature I don't understand?
>
Jeff
Because PHP (like most languages) makes its decision on whether a value
is a number or a string based on the first character. In this case you
must still use single quotes around the '1a' - or PHP will try to
interpret it as a numeric index.

Just because you use heredoc doesn't mean you can get rid of *all*
quotes - just many of them. You still need to be aware of what's going on.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Jerry Stuckle
Guest
 
Posts: n/a
#7: Nov 15 '08

re: heredocs and numbers in indexes


Jeff wrote:
Quote:
Luuk wrote:
Quote:
>Jeff schreef:
Quote:
>>Thomas Mlynarczyk wrote:
>>>Jeff schrieb:
>>>>
>>>>$content =<<<content
>>>>$array[1a]
>>>>content;
>>>>
>>>>Why is that? Is there a fix other than renaming the indexes?
>>>>{$array[1a]} or {$array['1a']} does not work.
>>>>
>>>{$array['1a']} works fine for me with PHP 5.2.0.
>>>
>> Thanks, I've got 5.2.6 here.
>>>
>> I've also some weird problem where with $ signs.
>>>
>>$D['some_var']= '$19.99';
>>>
>>echo <<<content
>>$D[some_var]
>>content;
>>>
>>yields this: .99
>>>
>>Just looked at the php change log but it's a bit hard to parse!
>>>
>> Jeff
>>>>
>>>Greetings,
>>>Thomas
>>>>
>>
>what OS are you running on ?
>
some flavor *nix.
>
head litenup.ca ?
>
It's a Blue Genesis server, not a great host, if you ask me!
>
Jeff
Works here, also. Is this the *exact* failing code?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Jeff
Guest
 
Posts: n/a
#8: Nov 15 '08

re: heredocs and numbers in indexes


Jerry Stuckle wrote:
Quote:
Jeff wrote:
Quote:
>Luuk wrote:
Quote:
>>Jeff schreef:
>>>Thomas Mlynarczyk wrote:
>>>>Jeff schrieb:
>>>>>
>>>>>$content =<<<content
>>>>>$array[1a]
>>>>>content;
>>>>>
>>>>>Why is that? Is there a fix other than renaming the indexes?
>>>>>{$array[1a]} or {$array['1a']} does not work.
>>>>>
>>>>{$array['1a']} works fine for me with PHP 5.2.0.
>>>>
>>> Thanks, I've got 5.2.6 here.
>>>>
>>> I've also some weird problem where with $ signs.
>>>>
>>>$D['some_var']= '$19.99';
>>>>
>>>echo <<<content
>>>$D[some_var]
>>>content;
>>>>
>>>yields this: .99
>>>>
>>>Just looked at the php change log but it's a bit hard to parse!
>>>>
>>> Jeff
>>>>>
>>>>Greetings,
>>>>Thomas
>>>>>
>>>
>>what OS are you running on ?
>>
> some flavor *nix.
>>
> head litenup.ca ?
>>
> It's a Blue Genesis server, not a great host, if you ask me!
>>
> Jeff
>
Works here, also. Is this the *exact* failing code?
As far as the original problem, yes. I ran a test script.

$a = '$19.99';

echo <<<this_data
$a
this_data;

I find that a stand alone works as expected. In the code block itself
it fails. There's a bit of PDO before and not much else.

This is on the "special projects" server at Blue Genesis because they
don't run the MySQL PDO drivers on their normal server. I wouldn't be
surprised if there was a connection.

In fact, my "fix" for both bits is this:

foreach($D as $key=>$value){
$value = str_replace('$','',$value); // remove $
$D['x' . $key] = $value; // don't start index with a number
}

funky.

Jeff
Jerry Stuckle
Guest
 
Posts: n/a
#9: Nov 15 '08

re: heredocs and numbers in indexes


Jeff wrote:
Quote:
Jerry Stuckle wrote:
Quote:
>Jeff wrote:
Quote:
>>Luuk wrote:
>>>Jeff schreef:
>>>>Thomas Mlynarczyk wrote:
>>>>>Jeff schrieb:
>>>>>>
>>>>>>$content =<<<content
>>>>>>$array[1a]
>>>>>>content;
>>>>>>
>>>>>>Why is that? Is there a fix other than renaming the indexes?
>>>>>>{$array[1a]} or {$array['1a']} does not work.
>>>>>>
>>>>>{$array['1a']} works fine for me with PHP 5.2.0.
>>>>>
>>>> Thanks, I've got 5.2.6 here.
>>>>>
>>>> I've also some weird problem where with $ signs.
>>>>>
>>>>$D['some_var']= '$19.99';
>>>>>
>>>>echo <<<content
>>>>$D[some_var]
>>>>content;
>>>>>
>>>>yields this: .99
>>>>>
>>>>Just looked at the php change log but it's a bit hard to parse!
>>>>>
>>>> Jeff
>>>>>>
>>>>>Greetings,
>>>>>Thomas
>>>>>>
>>>>
>>>what OS are you running on ?
>>>
>> some flavor *nix.
>>>
>> head litenup.ca ?
>>>
>> It's a Blue Genesis server, not a great host, if you ask me!
>>>
>> Jeff
>>
>Works here, also. Is this the *exact* failing code?
>
As far as the original problem, yes. I ran a test script.
>
$a = '$19.99';
>
echo <<<this_data
$a
this_data;
>
I find that a stand alone works as expected. In the code block itself
it fails. There's a bit of PDO before and not much else.
>
This is on the "special projects" server at Blue Genesis because they
don't run the MySQL PDO drivers on their normal server. I wouldn't be
surprised if there was a connection.
>
In fact, my "fix" for both bits is this:
>
foreach($D as $key=>$value){
$value = str_replace('$','',$value); // remove $
$D['x' . $key] = $value; // don't start index with a number
}
>
funky.
>
Jeff
I understand what you're saying, but I suspect there's something else
different in your code which is causing your problem (especially with
the fix you have).

But as it's part of a larger code base (which you don't show), it's hard
to tell just what is going on.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Closed Thread