unlink()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

unlink()

Post by guitarlvr »

I have a script which compares two files against each other. Once the comparison has been made, i want to delete the files from the server.

This does not work. It deletes the first and not the second:

Code: Select all

if ((!unlink($file1)) && (!unlink($file2))){
     echo 'files could not be deleted';
}
This, however, does work:

Code: Select all

unlink($file1);
unlink($file2);
if ((file_exists($file1)) || (file_exists($file2))){
	echo 'Files could not be deleted from server.';
}
Can anyone see why the if statement didn't work?

Wayne
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post by blackbeard »

Because as soon as the first file is deleted, the left side of the expression is going to be false. Unlink will return true when the file is deleted, but the ! will cause it to be false. As soon as the parser sees that the left side is false, it stops because it know the entire expression will be false, and the right side will never be executed.

You might try:

Code: Select all

if (!(unlink($file1) && unlink($file2)) {
     echo 'files could not be deleted';
}
I think that will work.
User avatar
guitarlvr
Forum Contributor
Posts: 245
Joined: Wed Mar 21, 2007 10:35 pm

Post by guitarlvr »

It still did the same thing with the code posted. but i tried:

Code: Select all

if (!((unlink($file1)) && (unlink($file2)))){
     echo 'files could not be deleted';
}
and that seemed to work. Thanks blackbeard!

Wayne
Post Reply