Re: Dot in array key name [message #169998 is a reply to message #169985] |
Mon, 04 October 2010 09:50 |
Captain Paralytic
Messages: 204 Registered: September 2010
Karma:
|
Senior Member |
|
|
On 3 Oct, 03:04, Martin <warwo...@gmail.com> wrote:
> August Karlstrom wrote:
>> On 2010-10-02 08:30, Martin wrote:
>>> $icons=array();
>
>>> $icon_name='myicon.png';
>
>>> if($icons[$icon_name]){
>>> // the image has already been loaded
>>> $icon=$icons[$icon_name];
>>> } else {
>>> $icon=imagecreatefrompng($icon_name);
>>> $icons[$icon_name]=$icon;
>>> }
>
>> In the code above you reset the icon array each time the script is run,
>> which is probably not what you want. You should also use the function
>> isset in the if-statement condition so it can handle all file names
>> correctly including "0" and "0.0" (both interpreted as boolean false):
>
>> if (!isset($icons[$icon_name])) {
>> $icons[$icon_name] = imagecreatefrompng($icon_name);
>> }
>> $icon = $icons[$icon_name];
>
>>> I'm trying not to load an image more than once if it is required more
>>> than once.
>
>>> Anyway - i have a bug where sometimes the wrong image is used and
>>> wondered whether the dot in the image's filename is invalid syntax for
>>> an array key name?
>
>> No, it's perfectly valid.
>
>> /August
>
>> --
>> The competent programmer is fully aware of the limited size of his own
>> skull. He therefore approaches his task with full humility, and avoids
>> clever tricks like the plague. --Edsger Dijkstra
Please do not top post (top posting fixed)
> Thanks for the replies.
>
> The code i posted was typed from memory and being a javascript bod i
> didn't get the syntax correct, but in the code i'm debugging i did use
> isset() and the array wasn't being reset within the loop.
It is a lot easier for us to help you with problems in your code if
you post the code that has problems rather than other arbitrary code
that you have made up on teh spot.
|
|
|