3

I have the following;

$foo;
$foo  = serialize($foo);
print_r($foo);

it gave me a result of N;

Now I do not know if this is unique to the system i am using or if this is a normal result. All I know is it was causing major problems with my site.

My question is... is this normal when serializing an undefined variable?If so can someone please explain why it outputs this result.Thank you for your time.

3
  • thats what I was thinking and I tried google to find any document on this but couldn't find anything. Commented Jun 16, 2012 at 3:48
  • 1
    Don't you think that serializing undefined data should have an undefined behavior? How about defining them before calling serialize? Commented Jun 16, 2012 at 3:50
  • yes I agree. I was debugging a colleagues work and it seemed that this was causing his/her/its errors. I set a default value to remedy the problem but it took me a while to find. I just kept getting the N; popping up in the database and causing some strange results. Commented Jun 16, 2012 at 3:52

2 Answers 2

6

It appears that the string N; is the serialize()d form of null. (See this codepad)
However, two things to note:

  1. You should be defining all variables before using them, even if you just declare them as null. Otherwise you'll get a notice about $foo being an undefined variable. Some argue that you can hide warnings, but this is poor practice. You should prevent them completely by using sensible defaults for all variables.
  2. Typically, one does not need to read the output of serialize(). You can store it on a DB, in a file, or in a memory cache system like Memcached, APC, or Redis. However, you will never need to understand what the output of serialize() means thanks to unserialize().
Sign up to request clarification or add additional context in comments.

Comments

2

It works as expected:

<?php

$foo;
$foo  = serialize($foo);
var_dump($foo); // Print the string "N"

$unserialized = unserialize($foo);
var_dump($unserialized); // Print NULL

But why are you looking at the output of serialize? This should not be relevant to your website. What matters is that unserialize gives you back the correct data.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.