Strange loss of string variable value when used 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
Jeroen Oosterlaar
Forum Commoner
Posts: 37
Joined: Sun Nov 06, 2005 4:12 pm

Strange loss of string variable value when used in array

Post 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]
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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?
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Post 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
Post Reply