我试图构建一个函数,可以为我检查一组字符串并找出它的空字符串(")是否,所以在玩了一段时间后,我创建了这个:
function blank() {
$args = func_get_args();
$result;
foreach ($args as $arg) {
if ($arg == "") {
$result = TRUE;
continue;
} else {
$result = FALSE;
return $result;
}
return $result;
}
return $result;
}
$name = "Hamed";
$email = "";
if (!blank($name, $email)){
$msg = "Not blank";
} else {
$msg = "It's blank!";
}
echo $msg;
它工作得很好!但现在的问题是我不明白它是如何工作的详细说明。每行如何工作,关键字如何继续,返回的结果输出在不同的代码勺中。谢谢
编辑:顺便说一句,在这种情况下,empty() 和 isset() 将不起作用,因为一旦我单击提交按钮,isset() 和 !empty() 的结果 = TRUE .
让我们在多次迭代中执行此操作:
首先,让我们删除明显多余的代码
<?php
function blank() {
$args = func_get_args();
// $result; - superfluous, that's a no-op
foreach ($args as $arg) {
if ($arg == "") {
$result = TRUE;
continue;
}
else {
$result = FALSE;
return $result;
}
/* code execution will never reach this point
because the if-branch jumps right back to the loop header
and the else-branch exists the function completely
therefore:
*/
// return $result; -superfluous,
}
return $result;
}
第二次迭代:删除不太明显的多余代码
function blank() {
$args = func_get_args();
foreach ($args as $arg) {
if ($arg == "") {
$result = TRUE;
// no need for "continue" here; there's nothing else in the loop (anymore)
// than the if-else branch
//continue;
}
else {
// no need to assign the return value to any variable here
// $result = FALSE;
// return $result;
return FALSE;
}
}
return $result;
}
所以,剩下的就是
function blank() {
$args = func_get_args();
foreach ($args as $arg) {
if ($arg == "") {
$result = TRUE;
}
else {
return FALSE;
}
}
return $result;
}
它遍历参数数组并一次又一次地执行 if-branch,一次又一次地设置 $result=TRUE - 直到 else-branch 执行一次,它只返回 FALSE。
如果 else-branch 从未对整个数组执行,则返回 $result,它只能为 TRUE(由 if-branch 设置)或未定义(当数组中没有元素时$args,因此 if- 和 else-分支都没有被执行)。
因此,您甚至可以进一步简化:
function blank() {
$args = func_get_args();
foreach ($args as $arg) {
if ($arg != "") {
// if there is one non-blank element in the arguments -> return false
return FALSE;
}
}
// there was no non-blank element in the arguments array ->
return true;
}
(我把边缘情况留给你....,例如调用blank()根本没有参数)
编辑:我把$args=func_get_args()
行留在那里(而不是foreach(func_get_args()....
因为新的(er)可变长度参数列表语法看起来像
function blank(...$args) {
foreach ($args as $arg) {
if ($arg != "") {
return false;
}
}
return true;
}
这是相同的代码,添加了一些注释以澄清事情
function blank() {
//put all arguments passed to function in $args array (allows any number of args)
$args = func_get_args();
$result; //initialize $result (not needed)
foreach ($args as $arg) { //Loop through $args
if ($arg == "") {
$result = TRUE;
continue; //Do not process any more line in this loop (continue to next item)
} else {
//if the arg is not empty return False
$result = FALSE;
return $result;
}
//This will never be reached
return $result;
}
//if all arguments were empty we will reach this point
//At that point $result will be True, so return True;
return $result;
}
这是一个优化,也许可以更容易理解函数的作用:
function blank() {
//put all arguments passed to function in $args array (allows any number of args)
$args = func_get_args();
foreach ($args as $arg) { //Loop through $args
if ( ! emtpy($arg) ) {
//Return False if we ever find a non empty argument
return False
}
}
//if all arguments were empty we will reach this point
//At that point $result will be True, so return True;
return True;
}
当然,还有许多其他方法可以重写它。
你的代码似乎对你有用,但实际上并非如此。如果它工作,它将输出每个$name和$email的结果。您的函数中有 3 个返回值,因此函数在此行停止,仅返回第一个参数的结果。我不会进一步解释,但我建议你继续学习 php 的基础知识。仔细阅读下面的代码,并与你的代码进行比较,并尝试了解您的问题所在。我们都从错误中吸取教训:)祝你好运
这是重写的代码:
<?php
function blank(){
$args = func_get_args();
$result = array();
foreach ($args as $key) {
if ($key == "") {
$result[] = array("$key", "It's blank");
}
else{
$result[] = array("$key", "Not Blank");
}
}
return $result;
}
$name = "Hamed";
$email = "";
$data = blank($name, $email);
print_r($data);
?>
输出:
Array (
[0] => Array ( [0] => Hamed [1] => Not Blank ),
[1] => Array ( [0] => [1] => It's blank )
)