Weird array behavior 
Author Message
 Weird array behavior

Look at the code below:
$aa = array("a1" => "r1", "a2" => "r2");
$b = "a1";
echo $aa['$b'];
How come it doesn't display "r1" ???
But if I change the last line to:
echo $aa[$b];
or
echo $aa["$b"];
it works fine. What is the difference?


Tue, 21 Jun 2005 10:01:56 GMT  
 Weird array behavior


Quote:
> Look at the code below:
> $aa = array("a1" => "r1", "a2" => "r2");
> $b = "a1";
> echo $aa['$b'];
> How come it doesn't display "r1" ???
> But if I change the last line to:
> echo $aa[$b];
> or
> echo $aa["$b"];
> it works fine. What is the difference?

because '$b' becomes the string '$b' while "$b" and $b become the string
'a1'. Variables inside a ' quoted string are not interpreted, in a " quoted
string are.

--
Tom Thackrey
www.creative-light.com

 Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------        
                http://www.usenet.com



Tue, 21 Jun 2005 10:21:55 GMT  
 Weird array behavior

Quote:

> Look at the code below:
> $aa = array("a1" => "r1", "a2" => "r2");
> $b = "a1";
> echo $aa['$b'];
> How come it doesn't display "r1" ???
> But if I change the last line to:
> echo $aa[$b];
> or
> echo $aa["$b"];
> it works fine. What is the difference?

Tom already gave you the answer, but I guess you really need to read all
details: http://php.net/types.string

Palahala



Tue, 21 Jun 2005 15:39:43 GMT  
 Weird array behavior

Quote:
>Look at the code below:
>$aa = array("a1" => "r1", "a2" => "r2");
>$b = "a1";
>echo $aa['$b'];
>How come it doesn't display "r1" ???
>But if I change the last line to:
>echo $aa[$b];
>or
>echo $aa["$b"];
>it works fine. What is the difference?

 See http://www.php.net/manual/en/language.types.string.php

 Hint:

echo '$b';
echo "$b";

 and see the difference.

--

http://www.andyhsoftware.co.uk/space | disk usage analysis tool



Tue, 21 Jun 2005 22:20:27 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Weird Array behavior

2. - Weird behavior with ODBC!

3. Weird memo behavior

4. Need Help With Weird Filter Behavior

5. W95 OSR 1, 2 and 2.5 and weird Clarion4a/OCX behavior

6. Weird control behavior ?!?

7. Weird 2.003 behavior

8. Weird Browse Behavior

9. Weird Browse behavior

10. weird Windows behavior

11. Weird Browse/form behavior X3

12. Epson Stylus 300 weird behavior

 

 
Powered by phpBB® Forum Software