在PHP中返回无值

时间:2019-06-22 22:34:27

标签: javascript php return

我是PHP和JavaScript的初学者,并且对“没有价值的回报”有疑问。

  1. #codeA #codeB 的含义相同吗? 我想知道“函数内部没有值的返回”的作用。

  2. #codeB #codeC 之间是否有区别? 我想知道示例代码中的“返回” “退出” 之间是否存在差异。

  3. 如果 #codeB #codeC 具有相同的功能,则首选哪种格式?

  4. 答案“ 1、2、3”在JavaScript中是否相同?

codeA

std::byte

codeB

function doFunction() {
 if(!conditionA) {
 //do something
 }
}

codeC

function doFunction() {
 if(conditionA) return;
 //do something
}

1 个答案:

答案 0 :(得分:3)

回答你

  1. 在您的有限示例中,它们在功能上可能是等效的,因此,是的,它们具有相同的含义(请参见下文)
  2. Return退出功能范围,Exit结束PHP执行(见下文)
  3. 请参阅数字1(请参见上文,然后请参见下文)
  4. 没有JS不是PHP,例如没有“退出”

以下:

许多不同之处在于之后发生的事情,例如:

codeA

function doFunction() {
 if(!conditionA) {
     //do something -- only runs when conditionA is false
 }else{
    //do something else -- only runs when conditionA is true
 }

  //do something something else -- runs rather true or false
  //this could be before
}

 doFunction();
//do something something something else  -- runs rather true or false

codeB

function doFunction() {
 //do something something else --  runs rather true or false

 if(conditionA) return;
 //do something -- only runs when conditionA is false
 //do something else -- only runs when conditionA is false

}

 doFunction();
//do something something something else  -- runs rather true or false

codeC

function doFunction() {
 //do something something else --  runs rather true or false

 if(conditionA) exit;
  //do something -- only runs when conditionA is false
  //do something else -- only runs when conditionA is false
} 

 doFunction();
//do something something something else  -- only runs when conditionA is false

因此,从有限的角度来看,它们是相同的,但是B在conditionA is false时不执行任何操作。对于C,如果确实是您的脚本,则结束,如果不结束,则在执行退出函数do something something something else后继续执行。

根据情况使用哪种。