views:

1374

answers:

5

Okay, maybe this is because I've been coding for over 24 hours and my eyes are glazing over, but I'm stumped here. Why does this happen:

<?php
session_start();
$tmp = "index";

echo "A: " . $_SESSION['page_loaded']['index']; // returns 1
echo "B: " . $_SESSION['page_loaded'][$tmp]; // is set, but is empty
?>

I feel like I'm missing something very basic here but I don't know what.

A: 

Try it with the var_dump function to see it’s value.

Gumbo
A: 

Try using:

$tmp = 'index'; // Note the single quotes.
//...
echo "B: " . $_SESSION['page_loaded'][$tmp];

PHP interprets double and single quoted strings slightly differently. Read these articles here and here as that might be what you are facing.

UPDATE: Just to clarify (given the comments below) my reasoning to check if using single or double quoted strings are effecting the Original Posters problem is shown in the example below where the two strings are not equal:

$var = 1;

$tmpA = 'index_$var'; // will resolve to index_$var
$tmpB = "index_$var"; // will resolve to index_1

if ( $tmpA === $tmpB)
  echo 'Identical';
else
  echo 'Not Identical!'; // <--- we get this because they are NOT identical.
QAZ
Don't think the type of quotes used would make a difference in this case.
Michael Angstadt
possibly, but its about all I can see from the code posted by the OP. why down voted? its a fair enough answer.
QAZ
php sees `$tmp = 'index'` as identical to `$tmp = "index"` in every way. Agree though the OP didn't post much in the way of code.
Crescent Fresh
crescentfresh: yes in the example given they are identical but the real code in question potentially may have more complex values which may be constructed in a way that the type of quotes effects it.
QAZ
The only difference between single and double quotes is that PHP parses variables within double quotes. That's it. Try this: if ("test" === 'test') echo 'Identical';
Encoderer
I'm tempted to notch this one up a level. While PHP <em>says</em> that "index" == 'index', I have found that it will occasionally forget.
Christopher W. Allen-Poole
+1  A: 

Where are you setting the following?

$_SESSION['page_loaded'][$tmp];

The following works:

<?php

    session_start();
    $tmp = "index";

    $_SESSION["page_loaded"][$tmp] = "Foo";

    echo "A: " . $_SESSION['page_loaded']["index"]; // foo
    echo "<br/>";
    echo "B: " . $_SESSION['page_loaded'][$tmp]; // foo

?>
Jonathan Sampson
Where he's setting it shouldn't matter. The first echo line returns "1", so the second echo line should return "1" as well.
Michael Angstadt
@mangst - It DOES matter. If it's not returning the expected value, then you need to look back to where you're setting it.
Jonathan Sampson
No it shouldn't! :P The issue is that both lines are accessing the same array element, but in different ways. Line #1 uses an in-line string and the line #2 uses a variable containing a string. Both strings are equal, so both should be returning the same value.
Michael Angstadt
@mangst - My updated example works, which naturally causes me to want to see where/how the variable is being stored. You can mock it if you like, but I can clearly see something taking place in my example.
Jonathan Sampson
A: 

you should use

 $_SESSION['page_loaded']['tmp'] = "index";

instead of

 $tmp = "index";

and to retrieve the value use this

  echo "B: " . $_SESSION['page_loaded']['tmp']; // the output is: B: index

(get rid of the dollar sign $)

ahmed
No, this wouldn't work. By using $_SESSION['page_loaded']['tmp'], you would be accessing a completely different array element.
Michael Angstadt
@mangst: it works for me!, I have added explanation, hope it will work
ahmed
+1  A: 

I have a feeling you haven't actually cut and pasted that code? Is there anything you're leaving out?

Encoderer