Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

javascript - 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 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

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

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...