# PHP rename not working



## DrP (Jul 23, 2005)

I am trying to rename a variable on the third line, below:

$testname1 = $newfilename;
$testname2 = $ImageName;
rename($ImageName, $newfilename);
$testname3 = $ImageName;

However, it isn't happening! To test it I am doing this:

<?php echo $testname1 ?>

<?php echo $testname2 ?>

<?php echo $testname3 ?>

If it was working then $testname 3 should be tha same as $testname1.
But it isn't! It's the same as $testname2, which means it hasn't been renamed!
What is going on? What have I not done here? I've spent ages looking at this and as far as I can see my code is fine.
Please...


----------



## covert215 (Apr 22, 2006)

Lets trace your logic using numbers instead of variables. Say $newfilename is 1 and $ImageName is 2.

$testname1 is assigned the value of 1.
$testname2 is assigned the value of 2.
$ImageName is renamed as $newfilename, which duplicates the variable $ImageName and makes $newfilename equal to 2.
$testname3 is assigned the value of 2.

It worked properly.


----------



## brendandonhu (Jul 8, 2002)

rename() does not change the value of any variable, it only renames the file.


----------



## DrP (Jul 23, 2005)

Thanks for that. I'd been trying to follow a book and it was obviously getting late. That and the fact the book is so poorly written.
Covert215: Even though we now know rename doesn't change the value of variables, if it did, where'd you get the duplicate from, anyway?
Thanks again,


----------

