views:

117

answers:

3

I'm doing a print_r on a array stored on a session variable and for some unknown reason it's adding a number after the array prints.

Example:

Array
(
    [0] => 868
    [userid] => 868
)
1

If I do a print_r directly in the function itself and before the variable gets stored on session variable, it doesn't add that number 1.

Solution:

Almost at the same time as Paolo answered my question correctly I found the causing code.

A simple echo on print_r

Well, there we go, sometimes I guess we blind for minutes and just don't see stuff. Oh we coders...daaa

A: 

Can you post the code you are using to do this around print_r? The most common reason for getting a 1 is when you try to print a boolean:

$my_bool = true;
print $my_bool; // will be printed as 1
print_r($my_bool); // will also be printed as 1
Paolo Bergantino
+1  A: 

I had the same issue. You're probably echoing out the return value of print_r() which is 'true'. You'll have to set print_r() to return the formatted text rather that its success or failure.

echo "Session: ".print_r($_SESSION,true)."<br />\n";
egon0119
Strange, I would have guessed this was the cause but the other answer (Paolo Bergantino) has been accepted.
Adam Backstrom
Thank You for your comments
Codex73
A: 

Beside using print_r or vardump, you should consider using FirePHP or PHP Quick Profiler to help you debugging and displaying variable's value.

With FirePHP, you can display the value into Firebug's console. Using, PHP Quick Profiler, there will be a console added at the bottom of the page that can be used to display any value that you need.

Donny Kurnia