Page 1 of 1

Strange loss of string variable value when used in array

Posted: Thu Nov 24, 2005 7:01 am
by Jeroen Oosterlaar
Weirdan | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
I have the following piece of (testing) code (for some implementation of the Spaw WYSIWYG-editor):

Code: Select all

$imgDirName = '' . $currDMDir . '';
echo $currDMDir . ",";
$tstStr = "a" .  $currDMDir . "b";
$imgDirName = $tstStr;
echo $imgDirName;
$spaw_imglibs = array(
  array(
    'value'   => '' . 'siteimg/'. '',
    'text'    => '' . $imgDirName . '',
  ),
);
This is some code from a file that is included by another file. In that other file the variable $currDMDir is set. As a test, the echo in the above code echoes the correct string value, for example "TEST". The $tstStr defines a string including $currDMDir, for example: "aTESTb". The echo of $imgDirName correctly echoes the string.

Now, when the value is echoed correctly, I expect the value of $imgDirName in the array definition to be set correctly as well. But this is not the case. Strangely enough the $imgDirName value in the array ($spaw_imglibs) is defined as "ab", so somehow the value of $currDMDir is omitted. (This value is used by a Spaw popup window).

How is this possible???

Thanks very much in advance!

Cheers,

Jeroen
Weirdan | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Thu Nov 24, 2005 7:23 am
by Skittlewidth
My first thought is that you shouldn't put a "," after the final element in an array, which you have done twice, and I'm surprised that this hasn't caused a parse error.

I'm not sure if this is the cause of the main problem you are experiencing though. What are the extra ''s either side of your array values supposed to do? They don't look like they're adding any whitespace.

Posted: Thu Nov 24, 2005 10:12 am
by Weirdan
Skittlewidth wrote:My first thought is that you shouldn't put a "," after the final element in an array, which you have done twice, and I'm surprised that this hasn't caused a parse error.
This syntax is perfectly acceptable in php.

Posted: Thu Nov 24, 2005 10:14 am
by Weirdan
Now, when the value is echoed correctly, I expect the value of $imgDirName in the array definition to be set correctly as well. But this is not the case. Strangely enough the $imgDirName value in the array ($spaw_imglibs) is defined as "ab", so somehow the value of $currDMDir is omitted. (This value is used by a Spaw popup window).
what does var_dump($spaw_imglibs) yield?

Posted: Thu Nov 24, 2005 10:16 am
by Skittlewidth
Weirdan wrote: This syntax is perfectly acceptable in php.
Really? Well you learn something new everyday. :)

Doesn't occur in any of the examples in the manual I've ever seen as far as I know, but what do I know?

edit:
php manual wrote: Having a trailing comma after the last defined array entry, while unusual, is a valid syntax.
Well I'll be... :D