dsideal2015 2013-10-09 15:54
浏览 49
已采纳

什么是将bool转换为文本值的最佳逻辑

I find myself writing code very similar to this regularly, in PHP and in C#

print isset($required) ? ($required ? "required" : "not required") : "not required";

Feels like I should be able to do something like this

print falseornull($required) ? "not required" : "required"

I could write a function to do this for me in PHP or C#, but I'm wondering if something already exists in either language? In C# I know there's string.IsNullOrEmpty to check for blank strings. Any equivalents for other types?

  • 写回答

3条回答 默认 最新

  • dongzg2006 2013-10-09 15:58
    关注

    Just as a code simplifier... If you're not checking for $required to be equal to a particular value, this:

    print isset($required) ? ($required ? "required" : "not required") : "not required"; 
    

    should be the same as this:

    print empty($required) ? "not required" : "required";
    

    empty() does just what you're after: "Determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value equals FALSE. empty() does not generate a warning if the variable does not exist. [...] The following things are considered to be empty:

    "" (an empty string)"

    http://us1.php.net/empty

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用lstm来预测股票价格
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上