简体   繁体   English

if语句中的PHP函数未运行

[英]PHP function within if statement not running

I have code similar to the following in PHP: 我在PHP中具有类似于以下代码:

if(move_uploaded_file($tempfile, $newfilelocation) && functionReturningFalse()) {} else {...}

Let's say the first function returns true (successfully moves the uploaded file) and there is some second function that returns false. 假设第一个函数返回true(成功移动了上传的文件),第二个函数返回false。 Why is the file not being moved? 为什么不移动文件? If I remove the second function call the file will move to its new location fine. 如果删除第二个函数调用,文件将移动到新位置。

***Edit: I am not asking why the code inside the braces {} would not be running. ***编辑:我不是在问为什么括号{}中的代码无法运行。 It is my thought that the php move_uploaded_file function should be moving a temporary file when it is called, even if the function called beside it (functionReturningFalse()) returns false. 我认为php move_uploaded_file函数在调用时应该移动一个临时文件,即使旁边调用的函数(functionReturningFalse())返回false。 This is not the case. 不是这种情况。 If the second function returns false, my file is not moved. 如果第二个函数返回false,则不会移动我的文件。

if (true && false) {
    echo 'both true';
} else {
    echo 'one or both false';
}

See the ! 看到了! (NOT) (不)

if (true && ! false) {
    echo 'both true';
} else {
    echo 'one or both false';
}

or be explicit... if(move_uploaded_file($tempfile, $newfilelocation) == true && functionReturningFalse() == false) { 还是明确的... if(move_uploaded_file($tempfile, $newfilelocation) == true && functionReturningFalse() == false) {

if((move_uploaded_file($tempfile, $newfilelocation)) && ! (functionReturningFalse())) {} else {...}

If I remove the second function call the file will move to its new location fine. 如果删除第二个函数调用,文件将移动到新位置。

You are on the wrong path. 您走错了路。 I don't know what's changing the behavior (or if it is changing at all), but in general , the return value of any function call on the right-hand side of the && will not change the behavior of the function call on the left-hand side of the && . 我不知道是什么改变了行为(或者根本没有改变),但是一般而言&&右侧的任何函数调用的返回值都不会改变函数调用的行为。 &&左侧

It is possible for the body of one function to undo or otherwise interfere with the results of a previous function call, but it doesn't sound like that's what is happening here. 可能的一个函数撤消或以其他方式与前一个函数调用的结果产生干扰,但它听起来并不像这就是发生在这里。 And even if this were the case, it wouldn't have anything to do with the second function's return value. 即使是这种情况,它也与第二个函数的返回值没有任何关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM