Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php serialize undefined variable

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.

like image 912
Martin Brody Avatar asked Dec 05 '25 13:12

Martin Brody


1 Answers

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().
like image 110
Bailey Parker Avatar answered Dec 08 '25 03:12

Bailey Parker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!