php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #15378 3d array variables not usable in double quotes
Submitted: 2002-02-05 00:30 UTC Modified: 2002-02-05 14:27 UTC
From: php dot net at chiefworks dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 4.1.1 OS: Linux 2.4, Apache 1.3.22
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: php dot net at chiefworks dot com
New email:
PHP Version: OS:

 

 [2002-02-05 00:30 UTC] php dot net at chiefworks dot com
<?php

$var = array(
    'name' => array(
        'first' => 'Caleb',
        'last' => 'Maclennan'
      )
  );

echo "My first name is $var[name][first]!";

?>

Acutal Result:
My first name is Array[first]!

Correct Result:
My first name is Caleb!

This get's really nasty when useing 3 dimentional arrays to put data in SQL querys. The above example can easily be done by takeing the variable out of the quotes and useing "." to add it to the end, but there are other cases where the only solution is to do like this:

<?php
$tempVar = $var[name][first];
echo "My name is $tempVar";
?>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-02-05 00:34 UTC] php dot net at chiefworks dot com
I should add that the the first level of the array works fine, it is just the second (& following) levels that cannot be used.

<?php

// This works:
$var[name] = 'Caleb';
echo "I am $var[name]";

// This does not:
$var[name][first] = 'caleb';
echo "I am $var[name][first]";
 [2002-02-05 03:21 UTC] edink@php.net
The correct syntax is:
echo "My first name is ".$var[name][first]."!";

You cannot have comples variables inside quotes.
 [2002-02-05 06:29 UTC] mfischer@php.net
This is not true. The following is perfectly valid:

  echo "My first name is {$var['name']['first']}!";
 [2002-02-05 11:48 UTC] php dot net at chiefworks dot com
edink@php.net -- I was aware of that syntax but there are some cases (sql querys, cookies & headers) where it is not possible to write it that way and the only solution WAS to assign it to a simpler variable.

mfischer@php.net -- Thanks. I guess that solves this problem. And this is documented where?
 [2002-02-05 14:27 UTC] torben@php.net
> edink@php.net -- I was aware of that syntax but there are 
> some cases (sql querys, cookies & headers) where it is not 
> possible to write it that way and the only solution WAS to 
> assign it to a simpler variable.

Not true--that syntax is possible anywhere you use strings, except for heredocs. 

Torben
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Wed Dec 11 16:01:26 2019 UTC