返回一个函数-PHP


Return in a function - PHP

即使我确实理解了一切都在做什么,我似乎也找不到return $totaal确实如此。低于该工作代码:

  <?php
  function Adding ($getal1, $getal2){
    $totaal = $getal1 + $getal2;
    return $totaal;
  }
  function Substract ($getal1, $getal2){
    $totaal = $getal1 - $getal2; 
    return $totaal;
  }
  $getal1=10;
  $getal2=2;
  echo Adding ($getal1, $getal2) . "<br>";
  echo Substract ($getal1, $getal2);
  ?>

我制作了我的函数,稍后通过回声调用它,但Return $totaal在函数中做什么。我从不叫它,但如果没有这个回报,我就会得到空白。

我脑子里一定是缺了某种逻辑。。。。

return作为单词本身所说的返回(返回)一些东西
在这种情况下,CCD_ 4或CCD_。

函数内部的变量通常不能在函数外部访问。
然而,有了return,你可以从函数内部获得一些东西来使用它

请记住,return将结束函数的执行。

Return将函数设置为字符串、int、bool等。

如果没有关键字return,函数就是一个黑洞。

功能内添加:

$totaal = $getal1 + $getal2;
return $totaal; // return returns $getal1+getal2

可以缩写为:

return $getal1 + getal2;

当你在做echo Adding($getal1, $getal2);

您实际上正在执行echo $getal1 + $getal2;

return语句立即结束当前函数的执行,并将其参数作为函数调用的值返回。

没有return,你可以像一样echo

function Adding ($getal1, $getal2){
    $totaal = $getal1 + $getal2;
    echo $totaal."<br>";
}
function Substract ($getal1, $getal2){
    $totaal = $getal1 - $getal2; 
    echo $totaal;
}
$getal1=10;
$getal2=2;
Adding ($getal1, $getal2);
Substract ($getal1, $getal2);

您可能了解return的作用,其中函数将产生一些值,而您需要它在其他地方。return语句停止执行并转到调用行。类似-

function hello() {
   return "welcome"; //return will send the produced value as a outcome of the function
}
$result = hello();// store the value returned by the function

现在$result将具有该值。

Return只会在调用函数后返回值,因此字符串:"Welcome Mr."和变量$name。当我调用函数("Piere")时,该变量将被赋予一个值。默认情况下,如果将字段留空,则会调用未知变量,否则会发生错误。因此:

    function naming ($name="Unknown"){
       return "Welcome Mr. $name";
    }
echo naming ("Pierre");

return定义函数的返回值。函数使用两个参数并返回一个值。第一个返回两个参数的和,第二个减去它们的值。例如

$a = Adding(3,2) // the value assigned to $a will be 5
$b = Substract(10,8) // the value assigned to $a will be 2

Return意味着无论返回什么值,都可以将其保存在变量中或通过串联使用,我将提供两个示例,一个带有Return,另一个没有

带return(注意:当调用return时,它会在返回值后停止执行函数):

Function AddFive($number) {
    Return $number += 5;
}
$number = AddFive(10); 
// $number now holds the value of 15 incase I need to manupilate it or do more stuff
Echo $number;

无退货:

Function AddFive($number) {
    Echo $number += 5;
}
AddFive(10); // this will echo out 15 immediately and I cannot manupilate it

如果你真的需要的话,最好使用return函数,因为这样你就把逻辑和表示分离了。然而,你可能很快就会编写的一些函数,比如影响数据库的函数,并不需要返回所有值,也就是说,如果你需要回显值或手动执行它,请使用return,如果它做了一些你不需要手动执行的事情,请不要使用它

当你用编程语言调用一个函数时,这意味着你从该函数中得到了一些经验,因为你在你的例子中给它提供了一些参数:$getal1$getal2,返回的函数会给你一个由参数上的特定操作得到的值。

每次你调用一个函数时,你实际上都在向它询问的响应

echo Adding ($getal1, $getal2);实际上与echo 12相同,但您使用该函数对动态数据执行相同的操作。

您需要将函数的结果绑定到一个变量并回显出来。由于return不会显示实际数据。要么是这样,要么你可以在函数中回显它。

所以它会有点像

function Adding($getal1,$getal2){
    $totaal = $getal1 + $getal2;
    return $totaal;
}
function Substract($getal1,$getal2){
    $totaal = $getal1 - $getal2; 
    return $totaal;
}
$getal1=10;
$getal2=2;
result1 = Adding($getal1,$getal2);
result2 = Substract($getal1 $getal2);
echo $result1." </br>";
echo $result2; 

您可以打印(echo)函数的结果,但更多。。。例如其他数学:

<?php $multiplication = Adding(5,10) * Substract(20,10); ?>

您应该存储在变量中,然后您可以检查条件是否。。。

<?php
  $add = Adding ($getal1, $getal2);
  $sub = Substract ($getal1, $getal2);
  if($add!=""){
     echo $add."<br>";
  }
  if($sub!=""){
     echo $sub;
  }
?>