Php 检查函数是否返回某些内容
如果函数Php 检查函数是否返回某些内容,php,function,Php,Function,如果函数validateCustomForm()不返回任何内容,则表示所有变量都已设置。我尝试使用empty(),但我认为,由于函数中存在某些东西,它总是会返回false 如何检查函数是否返回内容? 您应该在函数中返回一些内容,而不是echo。如下 <?php function validateCustomForm(){ $os0 = ""; $os1 = "5"; $os2 = "6"; $os3 = "5"; $error = false; if(!empty($os0
validateCustomForm()
不返回任何内容,则表示所有变量都已设置。我尝试使用empty()
,但我认为,由于函数中存在某些东西,它总是会返回false
如何检查函数是否返回内容?
您应该在函数中返回一些内容,而不是echo。如下
<?php
function validateCustomForm(){
$os0 = "";
$os1 = "5";
$os2 = "6";
$os3 = "5";
$error = false;
if(!empty($os0)){
//do nothing
}else{
$w = "width is missing";
$error = true;
}
if(!empty($os1)){
//do nothing
}else{
$h = "height is missing";
$error = true;
}
if(!empty($os2)){
//do nothing
}else{
$c = "color is missing";
$error = true;
}
if(!empty($os3)){
//do nothing
}else{
$q = "qty is missing";
$error = true;
}
return $error;
}//end function
$valid = validateCustomForm();
if(!empty($valid)){
echo "something is missing";
} else{
echo "all good";
}
?>
删除回显
s,只返回要回显的值,否则为空,或者如果愿意,返回false。我看不到任何返回
语句,因此$valid
将始终为空(空
)当您谈论return
时,函数Quick fix中必须有一个return
语句:@Dagon我喜欢它。希望你能回答,这样我就可以接受你的回答。
<?php
function validateCustomForm(){
$os0 = "";
$os1 = "5";
$os2 = "6";
$os3 = "5";
$errors = array();
if(empty($os0)){
$errors[] = "width is missing";
}
if(empty($os1)){
$errors[] = "height is missing";
}
if(empty($os2)){
$errors[] = "color is missing";
}
if(empty($os3)){
$errors[] = "qty is missing";
}
if(!empty($errors)) {
return $errors;
}
return TRUE;
}//end function
$valid = validateCustomForm();
if($valid !== TRUE){
echo "something is missing: ";
echo implode(',' , $valid);
} else{
echo "all good";
}
?>
<?php
function validateCustomForm(){
$os0 = "";
$os1 = "5";
$os2 = "6";
$os3 = "5";
$error = false;
if(!empty($os0)){
//do nothing
}else{
$w = "width is missing";
$error = true;
}
if(!empty($os1)){
//do nothing
}else{
$h = "height is missing";
$error = true;
}
if(!empty($os2)){
//do nothing
}else{
$c = "color is missing";
$error = true;
}
if(!empty($os3)){
//do nothing
}else{
$q = "qty is missing";
$error = true;
}
return $error;
}//end function
$valid = validateCustomForm();
if(!empty($valid)){
echo "something is missing";
} else{
echo "all good";
}
?>