首页 > 解决方案 > 如何指定 if 条件语句中返回的变量函数?

问题描述

我正在尝试根据 php 函数中返回的变量来编写我的逻辑。示例函数 xyz 有两个返回选项。所有变量 $a, $b, $c 将是动态的,返回值将根据用户在输入框中输入的内容是动态的。这些变量可以是 null 或字符串或数值。我想检查是否有任何值从 xyz 函数返回变量 $a 并根据返回的值应用逻辑。

谢谢!

function xyz($a, $b, $c){
  if (!empty($a)){
    return $a;
  }else{
    return $b;   
  }
}

$passingvalues = xyz(5, 2, 1);
if (!empty($passingvalues)){
   echo "there is value returning";
   //if returned value is $a and $a variable will have dynamic values depending on what user have entered.
   if ($a){
      echo "apply this logic";
   }else{
      echo "apply other logic";
   }
} else{
   echo "there is nothing returning from xyz";
}

标签: php

解决方案


我最终删除了该函数并在 if 语句上添加了更多逻辑。

if (!empty($passingvalues) ){
   echo "there is value returning";
   //if returned value is $a and $a variable will have dynamic values depending on what user have entered.
   if (!empty($a)){
      echo "apply this logic";
   }else{
      echo "apply other logic";
   }
} else{
   echo "there is nothing returning from xyz";
}

推荐阅读