Skip to content Skip to sidebar Skip to footer

Why The Prevoius Value Still Exists After Reinitialaize Variable - PHP

I came across a tutorial and when we reinitialize a variable that has a value , instead of removing the value , the variable still has the previous value . I the 3 rd var_dump i a

Solution 1:

$carname; does not in fact do anything. It's neither using the variable for any operation, nor is it assigning anything to it. In PHP variables are "initialised" by assigning something to them. If anything, $carname; is the same as echo $carname; without the echo, it's just a NOOP. In fact, the first line doesn't do anything either, the first var_dump triggers an Undefined variable carname notice.


Solution 2:

Writing:

$carName;

doesn't initialize the variable. It reads the variable but doesn't do anything with the value that it read.

You need to assign null to it:

$carName = null;

Solution 3:

Latest implementation of PHP totally ignores $carName; line.

$data;
echo $data;

Opcode.

line     # *  op                 fetch          ext  return  operands
-----------------------------------------------------------------------
   4     0  >   ECHO                                          !0
         1    > RETURN                                         1

http://3v4l.org/c3snU/vld#tabs

Other implementations treat this line as read operation and raise Undefined variable notice.

http://3v4l.org/c3snU#vhhvm-350


Solution 4:

If you need $carName to be null you should set it as null:

$carName = null;

$carName; alone doesn't anything.


Post a Comment for "Why The Prevoius Value Still Exists After Reinitialaize Variable - PHP"