Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 am expecting NULL , but it prints volvo . isn't this creating confusing things in the code .

$carName;
var_dump($carName);

$carName = "Volvo";
var_dump($carName);

$carName; 
var_dump($carName);



NULL
string(5) "Volvo"
string(5) "Volvo"

I also saw that javascript has the same behavior .

Is this a good feature of the language or a bad feature . According to my idea , this is bad , please explain . thanks in advance .

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
226 views
Welcome To Ask or Share your Answers For Others

1 Answer

$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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...