if语句中的php返回语句
也许我的问题有些简单或愚蠢,但我需要验证一下 我有一个php函数“function”,它在for循环中被重复调用:if语句中的php返回语句,php,if-statement,return,Php,If Statement,Return,也许我的问题有些简单或愚蠢,但我需要验证一下 我有一个php函数“function”,它在for循环中被重复调用: ... for($j=0; $j<$n_samples;$j++) { if($type=='triband') { functionA($arg1,$arg2); } } ... 。。。 for($j=0;$j您的返回将起作用,因为您对返回的结果不感兴趣。您只想继续for循环 如果您的代码构造测试了某些内容
...
for($j=0; $j<$n_samples;$j++) {
if($type=='triband') {
functionA($arg1,$arg2);
}
}
...
。。。
for($j=0;$j您的返回将起作用,因为您对返回的结果不感兴趣。您只想继续for循环
如果您的代码构造测试了某些内容,并且希望知道测试结果,则可以使用return false
让其余代码知道测试失败。函数将在return语句后停止。您可以在此处阅读更多信息:
例如,您可以执行以下操作来测试:
<?php
$_SESSION['a'] = "Naber";
function a(){
return;
unset($_SESSION['a']);
}
a(); // Let see that is session unset?
echo $_SESSION['a'];
?>
谢谢返回false
返回false
,布尔值。返回
将返回NULL
。也不会执行任何后续代码
因此,为了扩展RST的答案,两个返回都不会满足if
条件:
if(functionA($arg1,$arg2))
echo'foo';
else
echo'bar';
条
将被回显
如果您具备以下条件,这可能会很有用:
$return=functionA($arg1,$arg2);
if($return===NULL)
echo'Nothing happened';
elseif($return===false)
echo'Something happened and it was false';
else
echo'Something happened and it was true';
NULL
非常有用。您的函数可以完美地工作,但为了可读性起见,最好采用以下格式:
...
function functionA($arg1, $arg2) {
$wide_avg_txon = substr($bit_sequence,0,1);
// if is ON then skip execution of code inside this function
if ($wide_avg_txon != 0) {
echo " ---> is ON...<br />";
return;
}
echo " ---> is OFF...<br />";
// DO SOME STUFF!
}
...
。。。
函数function($arg1,$arg2){
$wide_avg_txon=substr($bit_序列,0,1);
//如果启用,则跳过此函数内代码的执行
如果($wide_avg_txon!=0){
echo“-->”处于打开状态…
”;
返回;
}
echo“-->”已关闭…
”;
//做点什么!
}
...
不同的是,你立即取消了“开”的资格条件是您不需要,并尽快退出该函数。然后该函数的其余部分将处理您想做的任何事情,而不是坐在if语句块中。两者都将起作用。如果您想以某种方式依赖它,您可以返回某个内容,例如if(function($arg1,$arg2==){}如果您希望它在
上为
返回true,因为true==1
。
$return=functionA($arg1,$arg2);
if($return===NULL)
echo'Nothing happened';
elseif($return===false)
echo'Something happened and it was false';
else
echo'Something happened and it was true';
...
function functionA($arg1, $arg2) {
$wide_avg_txon = substr($bit_sequence,0,1);
// if is ON then skip execution of code inside this function
if ($wide_avg_txon != 0) {
echo " ---> is ON...<br />";
return;
}
echo " ---> is OFF...<br />";
// DO SOME STUFF!
}
...